簡體   English   中英

Java mysql連接中的運行時錯誤

[英]runtime error in java mysql connectivity

現在,我收到運行時錯誤。 '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());
    }
  }

好的,我發現了問題。 您有Statement引用,它指向PreparedStatement對象。 但是PreparedStatement沒有使用的任何方法execute(String) ,它具有沒有任何參數的execute()方法。 那就是問題的根源。 而且,這不是使用PreparedStatement的正確方法。 您應該使用Statement或編寫查詢的方式,也可以在此處查看PreparedStatements工作方式。

以正確的方式(例如EG)使用java.sql.PreparedStatement

 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");



     }


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM