【问题标题】:How to properly initialize a create table servlet in JDBC?如何在 JDBC 中正确初始化创建表 servlet?
【发布时间】:2016-05-31 00:55:15
【问题描述】:

我在使用 Java 在我的数据库中创建表时遇到问题。我已经创建了正确创建数据库的方法:

/**
 * (@inheritDoc)
 * StartingServlet this Servlet, and create the database.
 * 
 * @throws javax.servlet.ServletException
 */
@Override
public void init() throws ServletException {
    if (SQLConnector.getConnection() == null){
        if (SQLConnector.tryConnection(this.getServletContext())){
            this.connection = SQLConnector.getConnection();
            this.initializeTableDisc();
            this.initializeTableAuthor();
            System.out.println("Connected and created DBs!");
        }
    }
}

/**
 * Method for creating a table, with statement.
 * @param sqlQuery what string to use to create a table
 */
private void createTable(String sqlQuery){
    try {
        Statement statement = this.connection.createStatement();
        statement.executeQuery(sqlQuery);
    } catch (SQLException ex) {

    }
}

/**
 * Creates a table for Discs.
 */
private void initializeTableDisc(){
    this.createTable("CREATE TABLE Discs (" +
            "discID INTEGER NOT NULL, " +
            "discName VARCHAR(20), " +
            "authorID INTEGER NOT NULL, discPrice FLOAT" +
            "PRIMARY KEY (discID) )");
}

/**
 * Creates a table for Authors.
 */
private void initializeTableAuthor(){
    this.createTable("CREATE TABLE Authors (" +
            "authorID INTEGER NOT NULL, " +
            "authorName VARCHAR(30), " + 
            "authorSurname VARCHAR(30), " + 
            "PRIMARY KEY (authorID) )");
}

使用 SQLConnector 类

public final class SQLConnector {

private static Connection connection = null;

public static boolean tryConnection(ServletContext context) {
    if (connection == null){
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            connection = DriverManager.getConnection(context.getInitParameter("url"), 
                    context.getInitParameter("login"), context.getInitParameter("password"));
        } catch (SQLException | NullPointerException | ClassNotFoundException ex){
            return false;
        }
    }
    return true;
}

/**
 * Try and close the connection.
 */
public static void closeConnection(){
    try {
        connection.close();
    } catch (SQLException ex) {
        ex.getMessage();
    }
}

/**
 * Getter for a connection.
 * 
 * @return connection which is used
 */
public static Connection getConnection(){
    return connection;
}
}

来自 web.xml 文件的我的 sn-p

<context-param>
    <param-name>url</param-name>
    <param-value>jdbc:derby://localhost:1527/musicdatabase</param-value>
</context-param>
<context-param>
    <param-name>login</param-name>
    <param-value>test</param-value>
</context-param>
<context-param>
    <param-name>password</param-name>
    <param-value>test</param-value>
</context-param>
<servlet>
    <description>Startup servlet</description>
    <servlet-name>StartingServlet</servlet-name>
    <servlet-class>pl.polsl.java.pawel.kucia.servlets.StartingServlet</servlet-class>
    <load-on-startup>0</load-on-startup>

我在这里做错了什么?调试时,它似乎没有正确添加数据库,它已正确初始化,因此可能是 SQL 语法或连接有问题。我真的没有想法。

【问题讨论】:

  • 尝试使用 executeUpdate() 而不是 executeQuery()。

标签: java servlets jdbc


【解决方案1】:

对于修改数据库的DDL和SQL语句需要用到

statement.executeUpdate(sql);

但在您的createTable 方法中,您使用statement.executeQuery()

【讨论】:

  • 为此浪费了 2 小时 :D
猜你喜欢
  • 1970-01-01
  • 2021-09-07
  • 2022-01-01
  • 2010-11-11
  • 2016-06-14
  • 1970-01-01
  • 2019-04-09
  • 2017-07-18
  • 2021-05-24
相关资源
最近更新 更多