gpt4 book ai didi

java - 如何在 JDBC 中正确初始化创建表 servlet?

转载 作者:行者123 更新时间:2023-12-01 10:22:26 25 4
gpt4 key购买 nike

我在用 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 文件片段

<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 语法或连接有问题。我真的没有主意了。

最佳答案

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

statement.executeUpdate(sql);

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

关于java - 如何在 JDBC 中正确初始化创建表 servlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35488570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com