用java做了一个简单的登陆窗口,已经连上数据库,如何用数据库验证登陆的用户名和密码? 10
我在主类中重写了ActionListener接口的ActionPerformed()方法,但具体怎么写就是弄不清楚。这是ActionPerformed方法。publicv...
我在主类中重写了ActionListener接口的ActionPerformed()方法,但具体怎么写就是弄不清楚。这是ActionPerformed方法。
public void actionPerformed(ActionEvent e)
{
try {
con=DriverManager.getConnection(url);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Statement s = null;
try {
s = con.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String username = null,password = null;
try {
ResultSet rs=s.executeQuery("SELECT * FROM text2");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(username.equals(txt1.getText())&&password.equals(txt2.getText()))
txt3.setText("登陆成功");
else
txt3.setText("登陆失败");
}
希望可以帮我在ActionPerformed()方法中重写数据库验证登陆用户名和密码的算法。 展开
public void actionPerformed(ActionEvent e)
{
try {
con=DriverManager.getConnection(url);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Statement s = null;
try {
s = con.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String username = null,password = null;
try {
ResultSet rs=s.executeQuery("SELECT * FROM text2");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(username.equals(txt1.getText())&&password.equals(txt2.getText()))
txt3.setText("登陆成功");
else
txt3.setText("登陆失败");
}
希望可以帮我在ActionPerformed()方法中重写数据库验证登陆用户名和密码的算法。 展开
2个回答
展开全部
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==Enter)
{
String username , password;
username = name.getText();
password = pwd.getText();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException ce)
{
JOptionPane.showMessageDialog(ss,ce.getMessage());
}
if(stu.isSelected())
{
try
{
Connection con = DriverManager.getConnection("jdbc:odbc:DS_121301_12","sa","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from stu where ID='"+username+"' and Pwd='"+password+"'");
while(rs.next())
{
if((rs.getString("ID").equals(username))&&(rs.getString("Pwd").equals(password)))
{
JOptionPane.showMessageDialog(ss,"登陆成功");
Students stu = new Students();
}
else
{
JOptionPane.showMessageDialog(ss,"登录失败");
}
}
{
if(e.getSource()==Enter)
{
String username , password;
username = name.getText();
password = pwd.getText();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException ce)
{
JOptionPane.showMessageDialog(ss,ce.getMessage());
}
if(stu.isSelected())
{
try
{
Connection con = DriverManager.getConnection("jdbc:odbc:DS_121301_12","sa","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from stu where ID='"+username+"' and Pwd='"+password+"'");
while(rs.next())
{
if((rs.getString("ID").equals(username))&&(rs.getString("Pwd").equals(password)))
{
JOptionPane.showMessageDialog(ss,"登陆成功");
Students stu = new Students();
}
else
{
JOptionPane.showMessageDialog(ss,"登录失败");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询