1

次のコードを使用して、Java を介してデータベースに接続しています。

import java.sql.*;

public class Can {

//Database Credentials
private static String userName = "root";
private static String password = "root";
private static String databaseName = "books";

static Connection connection = null; // manages connection
static Statement statement = null; // query statement
static ResultSet resultSet = null; // manages results

static Can can;


public static void main(String[] args) {

    can = new Can();
    ResultSetMetaData metaData;
    int numberOfColumns;


    try {
        connection = DriverManager.getConnection("jdbc:mysql://localhost/"+databaseName, userName, password);
        statement = connection.createStatement();
        connection.setAutoCommit(false);

        //creating the tables
        statement.executeUpdate("CREATE TABLE Instructor" + "(instructorID INT, instructorName VARCHAR(30), PRIMARY KEY (instructorID))");
        statement.executeUpdate("CREATE TABLE Lecture" + "(lectureID INT, lectureName VARCHAR(30), time TIME, credit INT, instructorID INT, PRIMARY KEY (lectureID))");
        statement.executeUpdate("CREATE TABLE LectureStudent" + "(lectureID INT, studentID INT, PRIMARY KEY (lectureID, studentID))");
        statement.executeUpdate("CREATE TABLE Student" + "(studentID INT, studentName VARCHAR(30), CGPA FLOAT, PRIMARY KEY (studentID))");
        statement.executeUpdate("CREATE TABLE Assignment" + "(assignmentID INT, deadline INT(30), lectureID INT, PRIMARY KEY (assignmentID))");
        statement.executeUpdate("CREATE TABLE Results" + "(resultID INT, grade FLOAT, PRIMARY KEY (resultID)");


        //inserting info. into the tables
        statement.executeUpdate("INSERT INTO Instructor VALUES('1', 'instructorName1')");
        statement.executeUpdate("INSERT INTO Instructor VALUES('2', 'instructorName2')");

        statement.executeUpdate("INSERT INTO Lecture VALUES('1', 'lectureName1', '20:16:40', '3', '1')");
        statement.executeUpdate("INSERT INTO Lecture VALUES('2', 'lectureName2', '20:33:20', '4', '1')");
        statement.executeUpdate("INSERT INTO Lecture VALUES('3', 'lectureName3', '20:33:25', '4', '2')");

        statement.executeUpdate("INSERT INTO LectureStudent VALUES('1', '100')");
        statement.executeUpdate("INSERT INTO LectureStudent VALUES('2', '100')");
        statement.executeUpdate("INSERT INTO LectureStudent VALUES('3', '100')");

        statement.executeUpdate("INSERT INTO Student VALUES('100', 'studentName1', '2.4')");
        statement.executeUpdate("INSERT INTO Student VALUES('200', 'studentName2', '2.5')");
        statement.executeUpdate("INSERT INTO Student VALUES('300', 'studentName3', '2.6')");

        statement.executeUpdate("INSERT INTO Assignment VALUES('1000', '1969-12-31 10:40:00.0', '1')");

        statement.executeUpdate("INSERT INTO Results VALUES('1', '90.0')");


        //commit the transaction
        connection.commit();

        connection.setAutoCommit(true);


        resultSet = statement.executeQuery("SELECT assignmentID, deadline, lectureID FROM Assignment");
        metaData = resultSet.getMetaData();
        numberOfColumns = metaData.getColumnCount();
        System.out.println("assignment");
        can.displayTable(resultSet, numberOfColumns, metaData);


        resultSet = statement.executeQuery("SELECT instructorID, instructorName FROM Instructor");
        metaData = resultSet.getMetaData();
        numberOfColumns = metaData.getColumnCount();
        System.out.println("instructor");
        can.displayTable(resultSet, numberOfColumns, metaData);


        resultSet = statement.executeQuery("SELECT lectureID, lectureName, time, credit, instructorID FROM LectureStudent");
        metaData = resultSet.getMetaData();
        numberOfColumns = metaData.getColumnCount();
        System.out.println("lecture");
        can.displayTable(resultSet, numberOfColumns, metaData);


        resultSet = statement.executeQuery("SELECT lectureID, studentID FROM LectureStudent");
        metaData = resultSet.getMetaData();
        numberOfColumns = metaData.getColumnCount();
        System.out.println("lecture_student");
        can.displayTable(resultSet, numberOfColumns, metaData);


        resultSet = statement.executeQuery("SELECT resultID, grade FROM Result");
        metaData = resultSet.getMetaData();
        numberOfColumns = metaData.getColumnCount();
        System.out.println("result");
        can.displayTable(resultSet, numberOfColumns, metaData);


        resultSet = statement.executeQuery("SELECT studentID, studentName FROM Student");
        metaData = resultSet.getMetaData();
        numberOfColumns = metaData.getColumnCount();
        System.out.println("student");
        can.displayTable(resultSet, numberOfColumns, metaData);


    } catch (SQLException e) {
        e.printStackTrace();
    }
    finally {
        // close the connection!
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {}
    }

}//END main



public void displayTable(ResultSet resultSet, int numberOfColumns, ResultSetMetaData metaData) {

    try {
        for(int i=1; i<=numberOfColumns; i++)
            System.out.printf("%-8s\t", metaData.getColumnName(i));
        System.out.println();

        while( resultSet.next () ) {

            for( int i=1 ; i<=numberOfColumns ; i++)
                System.out.printf("%-8s\t", resultSet.getObject(i));
            System.out.println();

        }
    } catch (Exception e) { }



}//END displayTable

}//END Can

テーブルの作成に関しては、うまく機能しています。しかし、問題は、プログラムを実行すると、構文エラーに関する次の奇妙なエラーが発生することです。

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
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2677)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1748)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1665)
at Can.main(Can.java:35)

何か案は?

ありがとう、C@N。

4

3 に答える 3

5

最後の Create ステートメントに閉じ括弧がありません:... PRIMARY KEY (resultID)");

そのはず:

    statement.executeUpdate("CREATE TABLE Results" + 
         "(resultID INT, grade FLOAT, PRIMARY KEY (resultID))");
                                                            ^
                                parenthesis added here -----|
于 2012-05-12T20:43:49.763 に答える
1

なぜ ID フィールドに '' を入れているのですか... INT 型の列です。挿入クエリを次のようにします

statement.executeUpdate("INSERT INTO Instructor VALUES(1, 'instructorName1')");

最後の create ステートメントに閉じ括弧がありません)

于 2012-05-12T20:40:51.780 に答える
1

エラーメッセージにあるように、35行目を見てください。貼り付けた行が正しい場合、それが最後の create Table ステートメントですか? そこのSQLに何か問題があります。

更新: @ypercube による質問へのコメントで指摘されているように: 閉じ括弧がありません!

于 2012-05-12T20:41:43.460 に答える