Eclipse+Mysql下开发,如何根据数据库中的一张表的内容生成数据库中的另一张表
数据库一张表1为:字段名称 | 字段类型 |类型大小|是否为主键| 所属表订单ID int 10 是 tbOrder
订单名称 varchar 60 否 tbOrder
广告名称 varchar 60 否 tbOrder
怎么写代码实现,根据表1自动生成数据库的另一张表tbOrder,这个表tbOrder的结构就是表1记录决定的:
表tbOrder为: 订单ID | 订单名称 | 广告名称 ,并且表1中每条记录的字段类型、类型大小、是否为主键等是怎样体现在 自动生成的表中的?
--------------------编程问答-------------------- sql建表呗,代码里把你需要的信息拿出来,拼到你建表的sql语句里不就完了。 --------------------编程问答-------------------- package com.bean.database;
import java.sql.Connection;
import java.util.List;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.bean.myhotel.dto.*;
public class JDBCPreparedStatementCreateExample {
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost/myhotel";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
public static void main(String[] args) {
try {
createTable();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
private static void createTable() throws SQLException {
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String createTableSQL = "CREATE TABLE DBUSER1("
+ "USER_ID NUMBER(5) NOT NULL, "
+ "USERNAME VARCHAR(20) NOT NULL, "
+ "CREATED_BY VARCHAR(20) NOT NULL, "
+ "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) "
+ ")";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(createTableSQL);
System.out.println(createTableSQL);
// execute create SQL stetement
preparedStatement.execute();
System.out.println("Table \"dbuser1\" is created!");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}
private static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER,
DB_PASSWORD);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
}这样建表,执行后,提示这种错误,怎么回事,求帮助啊 CREATE TABLE DBUSER1(USER_ID NUMBER(5) NOT NULL, USERNAME VARCHAR(20) NOT NULL, CREATED_BY VARCHAR(20) NOT NULL, CREATED_DATE DATE NOT NULL, PRIMARY KEY (USER_ID) )
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NUMBER(5) NOT NULL, USERNAME VARCHAR(20) NOT NULL, CREATED_BY VARCHAR(20) NOT NU' at line 1
补充:Java , Eclipse