在运行Java和SQL数据库连接时出现错误。

问题描述 投票:0回答:1
public class ConnectDB {

//DB connection variables

static Connection connection = null ;
static String databaseName = "";
static String url = "jdbc:mysql://localhost:3306/" +databaseName ;
static String username = "root";
static String password = "1AryahaileyM";


/**
 * @param args
 * @throws ClassNotFoundException 
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 * @throws SQLException 
 */
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
    // TODO Auto-generated method stub

    Class.forName("com.mysql.jdbc.Driver").newInstance();

    connection = DriverManager.getConnection(url,username , password);
    PreparedStatement ps = connection.prepareStatement("INSERT INTO 'studentdatabase'.'student'('name') VALUES ('Dilan Siri');");

    int status = ps.executeUpdate(); // if execution is sucessfull it will return integer value 

    if(status != 0 ){
        System.out.println("Databse is connected");
        System.out.println("Record was inserted");

    }
}

我在sql工作空间有一个数据库,名字叫student,有一个列名,当我编译java程序时,得到这个错误............。

Mon May 11 16:30:52 IST 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Exception in thread "main" 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 ''studentdatabase'.'student'('name') VALUES ('Dilan Siri')' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:403)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3933)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3869)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2675)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1915)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2136)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2070)
    at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5187)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2055)
    at ConnectDB.main(ConnectDB.java:33)
java mysql runtime-error database-connection
1个回答
0
投票

为了正确地完成这个任务,你应该删除 ' 围绕表和列的名称,并为您的语句使用参数。

String query = "INSERT INTO studentdatabase.student(name) VALUES (?)";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, "Dilan Siri");
© www.soinside.com 2019 - 2024. All rights reserved.