我有两个类,希望将一个int编号(第一个代码)插入数据库中。到目前为止,我发现的唯一一件事是准备好的语句,但是我希望每小时将来自“ freeparking”的int插入数据库(第二个代码)。我已经准备好了一个睡眠线程,可以让我的第二个代码每一个小时启动一次。但是我不确定如何在数据库中插入整数。感谢您的提前帮助!
private void setFreieparkplätze(int freeparking) {
this.freeparking = freeparking;
}
int freeparking = vehiclenumber.getParking();
}
static Connection connection = null;
static String databaseName = "";
static String url = "jdbc:mysql://localhost:3306/tiefgarage?autoReconnect=true&useSSL=false" + databaseName;
static String username = "";
static String password = "";
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(url, username, password);
**PreparedStatement ps = connection.prepareStatement("INSERT Into parkinglot(Numbers) VALUES (?)");** // ???
int status = ps.executeUpdate();
if (status != 0) {
System.out.println("Database connected");
System.out.println("Record was inserted");
}
答案 0 :(得分:0)
在数据库中添加整数的查询应该像这样:
INSERT Into parkinglot(Numbers) VALUES ("+ getFreeparking() +");
请注意,数字(表列)必须为整数类型, getFreeparking()只是freeparking int变量的获取器。