Java 创建表时出错
我正试图在数据库上创建一个新表,但出现了一个错误,显示: 线程“main”java.sql.SQLSyntaxErrorException中存在异常:表/视图“USERPRIVACYDB”不存在 代码如下:Java 创建表时出错,java,sql,derby,Java,Sql,Derby,我正试图在数据库上创建一个新表,但出现了一个错误,显示: 线程“main”java.sql.SQLSyntaxErrorException中存在异常:表/视图“USERPRIVACYDB”不存在 代码如下: public class UserPrivacyData { private static String password = null; private final static String USER = "root"; final String DRIVER
public class UserPrivacyData {
private static String password = null;
private final static String USER = "root";
final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
final String DATABASE_URL = "jdbc:derby://localhost:1527/USERPRIVACYDB;create=true;";
public void loadDB() throws SQLException {
try {
Class.forName(DRIVER); //load the driver
connection = DriverManager.getConnection(DATABASE_URL, USER, password);
statement = connection.createStatement();
} catch (Exception ex) {
ex.printStackTrace();
}
String sql = "insert into USERPRIVACYDB values('" + gender[rand.nextInt(2)] + "',"
+ " '" + privacyLevel[rand.nextInt(3)] + "', '" + age[rand.nextInt(3)] + "',"
+ " '" + presentEducationLevel[rand.nextInt(3)] + "',"
+ " '" + highestEducation[rand.nextInt(3)] + "', '"
+ everVictimOfCrimeString + "', "
+ " '" + crimeTypeString + "', "
+ " '" + shareWithThirdParty[rand.nextInt(2)] + "',"
+ " '" + reasonforChoiceofPrivacy[rand.nextInt(5)] + "')";
statement.executeUpdate(sql);
// }
}
}
有人能帮我吗?谢谢。您的JDBC连接URL和JDBC驱动程序类不匹配:
final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
final String DATABASE_URL = "jdbc:derby://localhost:1527/USERPRIVACYDB;create=true;";
格式为JDBC:derby://hostname:port/path/to/db
用于Derby的客户机-服务器配置,因此如果要使用此配置,需要将驱动程序更改为org.apache.Derby.jdbc.ClientDriver
,然后将derbyclient.jar
放入CLASSPATH
中
JDBC驱动程序类org.apache.derby.JDBC.EmbeddedDriver
用于derby的嵌入式配置,因此如果您想使用此配置,您需要将JDBC连接URL更改为JDBC:derby:path/to/db
,并将derby.jar
放在类路径中
这可能不是代码的全部问题,但是纠正它会很好,并且会帮助您明确如何构建和部署应用程序的计划
有关基本Derby配置的更多一般背景信息,请参阅:演示如何获取此错误消息的代码在哪里?请阅读《谢谢Liam》。代码如下:公共类UserPrivacyData{private static String password=null;private final static String USER=“root”;final String DRIVER=“org.apache.derby.jdbc.EmbeddedDriver”;final String DATABASE_URL=“jdbc:derby://localhost:1527/USERPRIVACYDB;create=true;“public void loadDB()引发SQLException{try{Class.forName(DRIVER);//加载驱动程序连接=DriverManager.getConnection(数据库URL、用户、密码);语句=connection.createStatement();}catch(异常ex){ex.printStackTrace();}insert into USERPRIVACYDB
-没有具有该名称的表。您需要运行create table insert into USERPRIVACYDB(…)
在您可以使用它之前。请:学习如何正确使用PreparedStatement
非常感谢。我将探讨您的选择。虽然我认为下面的语句会自动创建这样的表。