我正在尝试将数据插入数据库。我创建了Const类:
public class Const {
public static final String USER_TABLE = "users";
public static final String USER_ID = "idusers";
public static final String USER_FIRSTNAME = "firstname";
public static final String USER_LASTTNAME = "lastname";
public static final String USER_USERNAME = "username";
public static final String USER_PASSWORD = "password";
}
我检查了一下,列表上的名称与列名称相同,所以没有错误。 另外,我重新检查了此类中的配置,一切似乎都正确:
public class Configs {
protected String dbHost = "localhost";
protected String dbPort = "3306";
protected String dbUser = "root";
protected String dbPass = "*******";
protected String dbName = "prog";
}
这是DatabaseHandler类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Databasehandler extends Configs {
Connection dbConnection;
public Connection getDbConnection() throws ClassNotFoundException, SQLException {
String connectionString = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName;
Class.forName("com.mysql.cj.jdbc.Driver");
dbConnection = DriverManager.getConnection(connectionString, dbUser, dbPass);
return dbConnection;
}
public void signUpUser(String firstName, String lastName, String userName, String password) {
String insert = "INSERT INTO" + Const.USER_TABLE + "(" + Const.USER_FIRSTNAME + "," + Const.USER_LASTTNAME + "," + Const.USER_USERNAME + "," + Const.USER_PASSWORD + ")" + "VALUES(?,?,?,?)";
try {
PreparedStatement prSt = getDbConnection().prepareStatement(insert);
prSt.setString(1, firstName);
prSt.setString(2, lastName);
prSt.setString(3, userName);
prSt.setString(4, password);
prSt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
正在运行时,出现此错误,但未在结果网格中接收结果:
Mon Sep 24 21:19:42 GET 2018 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.
java.sql.SQLSyntaxErrorException: Table 'prog.intousers' doesn't exist
答案 0 :(得分:2)
在INTO之后您缺少空格:
INTO " + Const.USER_TABLE