当前位置: 动力学知识库 > 问答 > 编程问答 >

runtime error in java mysql connectivity

问题描述:

Now i get a run time error. 'java.lang.UnsupportedOperationException:Not yet implemented'

how can i restore it.

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

}

}

网友答案:

Ok i found the problem. You have Statement reference which points to PreparedStatement object. But PreparedStatementdoesn't have any method execute(String) which you are using.It has method execute() without any argument. Thats the source of problem. Moreover this is not the right way to use PreparedStatement. You should use either Statement , the way you have written the query , or you can see how PreparedStatements works here .

网友答案:

Use java.sql.PreparedStatement in a correct way, EG:

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

Set the variables:

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

UPDATE:

An EG as requested:

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



     }


}
分享给朋友:
您可能感兴趣的文章:
随机阅读: