gpt4 book ai didi

java - 无法找出 JDBC 错误

转载 作者:行者123 更新时间:2023-11-29 17:54:23 27 4
gpt4 key购买 nike

我正在创建一个项目来为我的编码类创建数据库。但是,当我运行这段代码时

import java.sql.*;

public class addTable {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://LocalHost/internData";

// Database credentials
static final String USER = "root";
static final String PASS = "password";

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");

//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");

//STEP 4: Execute a query
System.out.println("Creating table in given database...");
stmt = conn.createStatement();

String sql = "CREATE TABLE TREFYY " +
"(EntryNumber INTEGER not NULL, " +
" Date VARCHAR(255), " +
" CriterionNum INTEGER not NULL, " +
" Score VARCHAR(255) " ;

stmt.executeUpdate(sql);
System.out.println("Created table in given database...");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}

我收到此错误

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

如何解决此错误?从所有指南来看,我认为 SQL 似乎是正确的,但我想我遗漏了一些东西。

最佳答案

String sql = "CREATE TABLE TREFYY " +
"(EntryNumber INTEGER not NULL, " +
" Date VARCHAR(255), " +
" CriterionNum INTEGER not NULL, " +
" Score VARCHAR(255) " ;

您在 sql 语句末尾缺少结束 ):

String sql = "CREATE TABLE TREFYY " +
"(EntryNumber INTEGER not NULL, " +
" Date VARCHAR(255), " +
" CriterionNum INTEGER not NULL, " +
" Score VARCHAR(255))" ;

关于java - 无法找出 JDBC 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48995394/

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