现在我遇到了运行时错误。 '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());
}
}
答案 0 :(得分:1)
好的,我发现了问题。您有Statement
引用指向PreparedStatement
对象。但是PreparedStatement
没有您正在使用的任何方法execute(String)
。它没有任何参数的方法execute()
。这是问题的根源。此外,这不是使用PreparedStatement
的正确方法。您应该使用Statement
,编写查询的方式,也可以查看PreparedStatements
的工作方式here。
答案 1 :(得分: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");
}
}