用Java链接⃠ั 13:47:00 create database book_bar; use book_bar; create table reader( ID_card char(18), phone char(11), name varchar(6), primary key (ID_card,phone) ); create table VIP( ID_VIP char(8), categories varchar(10), discount double, amount double, primary key (ID_VIP) ); create table staff( ID_job char(8), phone char(11), name varchar(6), ID_card char(18), station varchar(6), salary double, state varchar(5), primary key (ID_job) ); create table book( book_number char(20), name varchar(20), book_kind varchar(10), book_location varchar(10), book_inventory int, book_price double, book_state varchar(5), book_purchase_time datetime, book_sale_time datetime, primary key (book_number) ); create table teaMenu( serial_number char(8), kind varchar(8), inventory int, listing_time datetime, off_shelf_time datetime, discount double, primary key (serial_number) ); ⃠ั 13:47:15 create database book_bar; use book_bar; create table reader( ID_card char(18), phone char(11), name varchar(6), primary key (ID_card,phone) ); create table VIP( ID_VIP char(8), categories varchar(10), discount double, amount double, primary key (ID_VIP) ); create table staff( ID_job char(8), phone char(11), name varchar(6), ID_card char(18), station varchar(6), salary double, state varchar(5), primary key (ID_job) ); create table book( book_number char(20), name varchar(20), book_kind varchar(10), book_location varchar(10), book_inventory int, book_price double, book_state varchar(5), book_purchase_time datetime, book_sale_time datetime, primary key (book_number) ); create table teaMenu( serial_number char(8), kind varchar(8), inventory int, listing_time datetime, off_shelf_time datetime, discount double, primary key (serial_number) );
时间: 2024-03-12 19:45:38 浏览: 59
以下是使用Java链接MySQL并执行上述SQL语句的示例代码:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQLConnector {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/";
String dbName = "book_bar";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "yourpassword"; // 替换为你的MySQL密码
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url + dbName, userName, password);
Statement stmt = conn.createStatement();
String sql = "create table reader( ID_card char(18), phone char(11), name varchar(6), primary key (ID_card,phone) );";
stmt.executeUpdate(sql);
sql = "create table VIP( ID_VIP char(8), categories varchar(10), discount double, amount double, primary key (ID_VIP) );";
stmt.executeUpdate(sql);
sql = "create table staff( ID_job char(8), phone char(11), name varchar(6), ID_card char(18), station varchar(6), salary double, state varchar(5), primary key (ID_job) );";
stmt.executeUpdate(sql);
sql = "create table book( book_number char(20), name varchar(20), book_kind varchar(10), book_location varchar(10), book_inventory int, book_price double, book_state varchar(5), book_purchase_time datetime, book_sale_time datetime, primary key (book_number) );";
stmt.executeUpdate(sql);
sql = "create table teaMenu( serial_number char(8), kind varchar(8), inventory int, listing_time datetime, off_shelf_time datetime, discount double, primary key (serial_number) );";
stmt.executeUpdate(sql);
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
在代码中,我们使用了Java的JDBC API,通过连接MySQL数据库并执行SQL语句来创建数据库和表。需要注意的是,你需要替换代码中的`password`变量为你的MySQL密码。
阅读全文