gpt4 book ai didi

java - 我对java和sql数据库做错了什么?

转载 作者:行者123 更新时间:2023-11-29 04:51:03 26 4
gpt4 key购买 nike

我正在尝试使用 jdbc:derby 数据库创建一个表。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

public class DBCONNECT {

Connection con;

public DBCONNECT()
{
connect();
}

private void connect()
{
try {
con= DriverManager.getConnection("jdbc:derby:relative/db/dubbydata.db;create=true;","APP","");
PreparedStatement st= con.prepareStatement("SELECT * FROM WF");
ResultSet res=st.executeQuery();
while(res.next()){}
st.close();
System.out.println("dubbyloop.DBCONNECT.connect()");
} catch (SQLException ex) {
try {
Statement stmt = con.createStatement();
stmt.executeQuery("CREATE TABLE WF (FILEPATH CHAR(254) NOT NULL, FILESIZE LONG VARCHAR, WFDATA CLOB(2147483647), PRIMARY KEY (FILEPATH));");
System.out.println("CREATED");
} catch (SQLException ex1) {
Logger.getLogger(DBCONNECT.class.getName()).log(Level.SEVERE, null, ex1);
}

System.out.println(ex);
}
}



}

但我总是得到这个错误:

SCHWERWIEGEND: null
java.sql.SQLSyntaxErrorException: Syntaxfehler: Encountered ";" at line 1, column 118.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.executeQuery(Unknown Source)
at myproject.DBCONNECT.connect(DBCONNECT.java:48)

我不知道我做错了什么。我更改了创建查询中的所有内容。我删除了分号。我删除了 Clob 甚至整个 Clob 的大小。不管我做什么我总是得到同样的错误,不知道出了什么问题。

最佳答案

    stmt.executeUpdate("CREATE TABLE WF ("
+ "FILEPATH VARCHAR(254) NOT NULL, "
+ "FILESIZE LONG, "
+ "WFDATA CLOB(2147483647), "
+ "PRIMARY KEY (FILEPATH))");

VARCHAR 太多了,不需要分号。 CHAR 用空格填充,所以 VARCHAR 更好。

@MickMnemonic 正确地提到应该调用 executeUpdate 而不是 executeQuery。

关于java - 我对java和sql数据库做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35395937/

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