java - 试图在 java netbeans 中创建表 sql
问题描述
我正在尝试按照教程使用netbeans在java中创建一个数据库表,但是我没有成功,当我运行它确实符合的代码并且没有错误但是当我连接到数据库表只是不显示thanx的帮助
公共类DataBaseDAO {
private Properties dbProperties;
private Connection dbConnection;
private String dbURL;
private final String createSalesRecordTableSQL = "create table APP.SALES_RECOREDS("
+ "SN INTEGER NOT NULL PRIMARY KEY"
+ "INVOICE_NO VARCHAR(15) NOT NULL,"
+ "DESCRIPTION VARCHAR(150) NOT NULL,"
+ "PRICE_SOLD DOUBLE NOT NULL,"
+ "CUSTOMER_NAME VARCHAR(150) NOT NULL"
+ ")";
public DataBaseDAO() {
try {
this.dbProperties = this.loadDBProperties();
String driverName = this.dbProperties.getProperty("derby.driver");
this.loadDatabaseDriver(driverName);
if(!this.dbExists()){
this.createDatabase();
}
} catch(IOException xcp) {
xcp.printStackTrace(System.err);
}
}
public final Properties loadDBProperties() throws IOException {
InputStream dbPropInputStream = DataBaseDAO.class.getResourceAsStream("Configuration.properties");
this.dbProperties = new Properties();
this.dbProperties.load(dbPropInputStream);
return this.dbProperties;
}
public void loadDatabaseDriver(String driverName) {
try {
Class.forName(driverName);
} catch(ClassNotFoundException xcp) {
xcp.printStackTrace(System.err);
}
}
private boolean dbExists() {
String dbLocation = this.getDatabaseLocation();
File dbFileDir = new File(dbLocation);
return dbFileDir.exists();
}
public String getDatabaseLocation() {
String dbLocation = System.getProperty("app.system.database");
return dbLocation;
}
private boolean createDatabase() {
boolean bCreated = false;
this.dbConnection = null;
this.dbURL = this.getDatabaseURL();
System.out.println("Database URL: " + this.dbURL);
this.dbProperties.put("create","true");
try {
this.dbConnection = DriverManager.getConnection(dbURL, dbProperties);
bCreated = this.createSalesRecordsTable(this.dbConnection);
} catch(SQLException xcp) {
xcp.printStackTrace(System.err);
}
this.dbProperties.remove("create");
return bCreated;
}
public String getDatabaseURL() {
String derbyURL = this.dbProperties.getProperty("derby.url");
String databaseDir = System.clearProperty("app.system.database");
this.dbURL = (derbyURL + databaseDir);
return this.dbURL;
}
private boolean createSalesRecordsTable(Connection dbConnection) throws SQLException {
Statement statement = dbConnection.createStatement();
statement.execute(this.createSalesRecordTableSQL);
System.out.println("Sales records table was successfully created");
return true;
}
}
解决方案
推荐阅读
- python - Selenium 与 Python:如何在具有已知类的元素中选择标签
- python - 使用 Webargs Flaskparser 验证 URL 路径和查询参数中的变量参数
- python - 在 tkinter 计算器中显示减号和加号
- node.js - 在网页上发送 Apache Kafka 数据
- http-headers - 如何让浏览器将所有 HTTP 标头传递给 ServiceWorker?
- python - 在python中对导入语句进行分组
- javascript - 如何在 Express 中使用 Handlebars?
- python - 当我检查硬退回电子邮件时,python 上的条件
- android - 从 React Native 项目生成 apk 时遇到问题?
- vue.js - 无法在控制台中输出全局属性