首页 > 解决方案 > 试图在 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;
}
}

标签: java

解决方案


推荐阅读