0

我正在尝试连接到 java 中的 MYSQL 数据库并插入新数据。如果我使用jconnector(jar 文件)版本5.1.47,则会在executeUpdate(String s). 如果我使用版本 8 的 jar 文件,则会在Connection myCon = DriverManager.getConnection("jdbc...").

我在这两种情况下都使用 jdk 1.8.0(版本 8)。没有错误消息,只有异常。这是我在 main 函数中的代码。我可以在 phpmyadmin 上验证数据库是否正确(我正在使用 xampp 访问它)。

try {
    final String driver = "com.mysql.cj.jdbc.Driver";
    Class.forName(driver);
    String databaseP = "1234";

    Connection myCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDatabase","root",databaseP);
    Statement St = myCon.createStatement();
    String sql = "insert into clients (username, password) values ('marco', 'polo')";
    St.executeUpdate(sql);
//            System.out.println("Insert complete!");
//            //ResultSet rs = St.executeQuery("select * from Clients;");
//
////            while(rs.next()){
////                System.out.println(rs.getString(""));
////            }
    St.close();
    myCon.close();
}
catch(SQLException e){
    System.out.println(e.getErrorCode());
}

如果在Connection myCon: 错误代码为 0 处引发异常,则堆栈跟踪为[Ljava.lang.StackTraceElement;@1574691

如果在executeUpdate(sql): 错误代码为 1364 处引发异常,则堆栈跟踪为[Ljava.lang.StackTraceElement;@b48321

4

0 回答 0