0

现在我得到一个运行时错误。'java.lang.UnsupportedOperationException:尚未实现'我该如何恢复它。

 private void btnlogActionPerformed(java.awt.event.ActionEvent evt) {

    user=txtuser.getText();
    char[] pass=jPasswordField1.getPassword();
    String passString=new String(pass);
    try{
      Connection con = createConnection();
      String sql = "INSERT INTO login(username,Password) VALUES ('" + user + "','" + passString + "')";
      Statement st = con.prepareStatement(sql);
      st.executeUpdate(sql);
    }
    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());
    }
  }
4

2 回答 2

1

好的,我发现了问题。您有Statement指向PreparedStatement 对象的引用。但是PreparedStatement没有execute(String)你正在使用的任何方法。它有execute()没有任何参数的方法。这就是问题的根源。而且这不是正确的使用方式PreparedStatement。您应该使用您编写查询的方式,或者您可以在此处Statement查看其PreparedStatements工作原理。

于 2013-01-10T13:36:56.140 回答
0

正确使用java.sql.PreparedStatement,EG:

 java.sql.PreparedStatement statement= con.prepareStatement("delete from song where no=(?)");

设置变量:

    statement.setInt(1,id);
    statement.setInt(2,...);
    sta...

更新:

按要求提供 EG:

import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class Insert {
public static void main(String args[]) throws ClassNotFoundException, SQLException    {


insert("e:/helloJDBC");

              }
public static void insert(String path) throws ClassNotFoundException, SQLException
{int id=1;


         Connection con = null;
  Statement stmt = null;
    String dbUrl = "jdbc:mysql://127.0.0.1:3306/song";//your db
 String dbClass = "com.mysql.jdbc.Driver";

 Class.forName(dbClass);
con = DriverManager.getConnection(dbUrl,"root","sesame");//enter reqd. fields
    java.sql.PreparedStatement statement= con.prepareStatement("insert into song values(?,?)");//Whatever your query
    statement.setInt(1,id);
    statement.setString(2,path);//set as per the order of ? above
    statement.execute();
    System.out.println("1 row effected");



     }


}
于 2013-01-10T13:47:43.333 回答