簡體   English   中英

為什么我收到此sql錯誤?

[英]Why I am receiving this sql error?

我開始了我的Java代碼在NetBeans中,我想在我插入一些數據numbersdb位於數據庫表firstdb 我在運行程序時遇到問題。

我的代碼是這樣的:

public static void main(String [] arg){
    try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Driver loaded!");
        Connection dbc = DriverManager.getConnection("jdbc:mysql://localhost:3306/firstdb", "root","root");
        System.out.println("Connection to database succeded!");

        boolean evening = false;
        int n100 = 1;
        int n10 = 2;
        int n1 = 3;
        int wn = 123;
        int month = 0;
        int day = 0;
        int year = 0;
        java.sql.Date date_released = new java.sql.Date(Calendar.getInstance().getTime().getTime());

        String query =  "insert into numbersdb (hundreds_place, tens_place, ones_place, whole_number, evening, date_released, day, month, year) "+
                        " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

        PreparedStatement preparedStatement = dbc.prepareStatement(query);
        preparedStatement.setInt(1, n100);
        preparedStatement.setInt(2, n10);
        preparedStatement.setInt(3, n1);
        preparedStatement.setInt(4, wn);
        preparedStatement.setBoolean(5, evening);
        preparedStatement.setDate(6, date_released);
        preparedStatement.setInt(7, day);
        preparedStatement.setInt(8, month);
        preparedStatement.setInt(9, year);

        preparedStatement.execute(query);       

        System.out.println("Query executed!!!");

    } catch (SQLException ex) {
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Cannot find the driver in the classpath!", e);
    } 
}

我在phpmyadmin中使用以下sql代碼創建了數據庫:

create table numbersdb (  
    hundreds_place int unsigned not null,  
    tens_place int unsigned not null,   
    ones_place int unsigned not null,   
    whole_number int unsigned not null,  
    evening boolean not null,  
    date_released date not null,  
    day int unsigned not null,  
    month int unsigned not null,  
    year int unsigned not null,  
    primary key (date_released)  
);

我的程序的輸出是這樣的:

驅動程序已加載! 與數據庫的連接成功! SQLException:您的SQL語法有錯誤; 在第1行的SQLState:42000中,檢查與您的MySQL服務器版本相對應的手冊,以在'?,?,?,?,?,?,?,?,?,?)'附近使用正確的語法
VendorError:1064

為什么我會收到這種例外情況?

preparedStatement.execute(query);

該行使用從Statement繼承的.execute(String) 所述方法僅執行給定查詢,該查詢現在包含? 的。

刪除參數以使用正確的方法,它將起作用。

preparedStatement.execute();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM