java 使用netbeans在java中更改密码程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15862832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change password program in java using netbeans
提问by Koneri
After all the fuss I used JAVA DB and now using apache Derby client driver to use and after finally entering my database i have a new problem
经过所有的大惊小怪,我使用了 JAVA DB,现在使用 apache Derby 客户端驱动程序来使用,最后进入我的数据库后,我遇到了一个新问题
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String uname = jTextField1.getText();
String strpass = jPasswordField1.getText();
String newpass = jPasswordField2.getText();
String conpass = jPasswordField3.getText();
try
{
DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
}
catch(SQLException ex1)
{
ex1.printStackTrace();
}
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
try
{
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/niiitusers");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select * from app.userlogin");
while(rs.next())
{
String usrname = rs.getString("username");
String passwd = rs.getString("password");
if(uname.equals(usrname) && strpass.equals(passwd))
{
if(newpass.equals(conpass))
{
Statement st1 = con.createStatement();
ResultSet i = st1.executeQuery("UPDATE app.userlogin SET password='newpass' where username='usname'");//query i am using to update the password
JOptionPane.showMessageDialog(null, "PASSWORD UPDATE SUCCESSFUL");
}
else
{
JOptionPane.showMessageDialog(null, "PLEASE CONFIRM PASSWORD");
}}
else if(uname.equals("") && strpass.equals("") && newpass.equals(""))
{
JOptionPane.showMessageDialog(null, "PLEASE ENTER ALL INFORMATION");
}
else
{
JOptionPane.showMessageDialog(null, "USERNAME NOT FOUND");
}
st.close();
con.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
java.sql.SQLException: executeQuery method can not be used for update.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.Statement.executeQuery(Unknown Source)
Its not performing the update operation. I am not sure where the problem is
它不执行更新操作。我不确定问题出在哪里
回答by david99world
it's not showing any output because your catch block has nothing in it.
它没有显示任何输出,因为您的 catch 块中没有任何内容。
First off, this needs to change...
首先,这需要改变......
catch (SQLException ex) {
e.printStackTrace();
} }
Secondly, I'd look at the obvious stuff, are you using the right credentials? Is the IP address correct for the db?
其次,我会看一些显而易见的东西,你是否使用了正确的凭据?数据库的 IP 地址是否正确?
Either way, the stacktrace will give you the answers you need.
无论哪种方式,堆栈跟踪都会为您提供所需的答案。
回答by Ravi kumar
dont use executeQuery for updating password. rather, use executeUpdate for updating. and it has no Resultset use it like this
不要使用 executeQuery 来更新密码。相反,使用 executeUpdate 进行更新。它没有结果集像这样使用它
if(newpass.equals(conpass))
{
Statement st1 = con.createStatement();
st1.executeUpdate("UPDATE app.userlogin SET password='newpass' where
username='usname'");
JOptionPane.showMessageDialog(null, "PASSWORD UPDATE SUCCESSFUL");
}
回答by karyton
remove rs and use executeUpdate()
instead of executeQuery()
删除 rs 并使用executeUpdate()
代替executeQuery()
the last else
statement is also unnecessary
最后else
一句也是不必要的