我在con = createConnection()时遇到语法错误;在netbeans ide中,我想将记录插入到mysql数据库中。怎么能纠正这个?
private void btnlogActionPerformed(java.awt.event.ActionEvent evt) {
user=txtuser.getText();
char[] pass=jPasswordField1.getPassword();
String passString=new String(pass);
**![con =createConnection();][1]**
try{
String str="INSERT INTO login(username,Password) VALUES ('" + user + "','" + passString + "')";
java.sql.PreparedStatement statement=con.prepareStatement(str);
statement.setString(1,user);
statement.setString(2,passString);
statement.executeUpdate();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Exception: "+ e.toString());
}
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://localhost/Stock?"+
"user=root&password=";
Connection con = DriverManager.getConnection(connectionUrl);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,"SQL Exception: "+ e.toString());
} catch (ClassNotFoundException cE) {
JOptionPane.showMessageDialog(null,"Class Not Found Exception: "+ cE.toString());
}
答案 0 :(得分:0)
什么是con
,你有createConnection()静态方法可以给你连接吗?
con =createConnection();
如果您使用PreparedStatement
并设置参数,则应在此查询中设置占位符
String str = "INSERT INTO login(username,Password) VALUES (?, ?)";
答案 1 :(得分:0)
试试这个,
String str="INSERT INTO login(username,Password) VALUES (?,?)";
java.sql.PreparedStatement statement=con.prepareStatement(str);
statement.setString(1,user);
statement.setString(2,passString);
statement.executeUpdate();