我在将数据存储到我在Netbeans中创建的sql数据库时遇到了问题。
String bladeSerial;
String bladeType;
LocalTime startTime1;
private void startButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Connection conn = null;
Statement st = null;
try {
conn = DriverManager.getConnection ("jdbc:derby://localhost:1527/db01", "Administrator", "admin"); //run procedure getConnection to connect to the database - see below
st = conn.createStatement(); //set up a statement st to enable you to send SQL statements to the database.
} catch (SQLException ex) {
Logger.getLogger(FormTwo1.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println ("Successful Connection");
String query = "insert into TB01(SERIAL,BLADETYPE,STARTT1) values ('+bladeSerial+', '+itemText+', '+(String.valueOf(startTime1))+')";
try (PreparedStatement pstmt = conn.prepareStatement(query)) {
pstmt.setString(1, bladeSerial);
pstmt.setString(2, bladeType);
pstmt.setString(3, String.valueOf(startTime1));
pstmt.executeUpdate();
} catch (SQLException ex) {
// Exception handling
Logger.getLogger(FormTwo1.class.getName()).log(Level.SEVERE, null, ex);
}
startTime1
变量以HH:mm:ss.SSS
格式保存。运行代码时,我收到错误:
java.sql.SQLSyntaxErrorException:语法错误:遇到":"在第1行,第65栏。
错误是指当时的冒号,但我不知道如何解决这个问题。
SERIAL
是VARCHAR(5)
,BLADETYPE
是VARCHAR(80)
,STARTT1
是VARCHAR(12)
。所有列都在DATA
表中。
答案 0 :(得分:4)
改为使用300px
,并使用PreparedStatement
方法设置参数:
set*