求一个检测用户名是否存在的java代码例子,SQL数据库最好了 有的发我Q278473051
1个回答
展开全部
这是access数据库的,网上有的,你把数据库改一下就可以了,在库中建立用户名和密码
class Login {
private Frame f;
private Label l1,l2,l3;
private TextField t1,t2;
private Button b1,b2;
Connection con=null;
Statement st=null;
ResultSet rs=null;
String sql=null;
String textName=null;
String textPassword=null;
public void showMessage()
{
JOptionPane.showMessageDialog(null, "帐号、密码不正确!");
}
public Login()
{
f=new Frame("登录");
l1=new Label("用户名:");
l2=new Label(" 密 码:");
l3=new Label("...");
t1=new TextField(10);
t2=new TextField(10);
b1=new Button("确定");
b2=new Button("取消");
}
public void init()
{
f.setLayout(new FlowLayout());
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(b1);
f.add(b2);
f.add(l3);
f.setSize(200,200);
f.setLocation(400,350);
f.setVisible(true);
b1.addMouseListener(new MyListener());
b2.addMouseListener(new MyListener());
f.addMouseListener(new MyListener());
}
class MyListener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
if(e.getSource()==b1)
{
textName=t1.getText();
textPassword=t2.getText();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:dsStudent");
Statement st=con.createStatement();
String sql="select * from admin where name='"+textName+"' and password='"+textPassword+"'";
rs=st.executeQuery(sql);
if(rs.next())
{
System.out.println("验证成功");
//System.out.println(sql);
info.setEnabled(true);
datab.setEnabled(true);
Login.this.dispose();
}
else
{
showMessage();
}
rs.close();
con.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
else if(e.getSource()==b2)
{
Login.this.dispose();
}
}
}
class Login {
private Frame f;
private Label l1,l2,l3;
private TextField t1,t2;
private Button b1,b2;
Connection con=null;
Statement st=null;
ResultSet rs=null;
String sql=null;
String textName=null;
String textPassword=null;
public void showMessage()
{
JOptionPane.showMessageDialog(null, "帐号、密码不正确!");
}
public Login()
{
f=new Frame("登录");
l1=new Label("用户名:");
l2=new Label(" 密 码:");
l3=new Label("...");
t1=new TextField(10);
t2=new TextField(10);
b1=new Button("确定");
b2=new Button("取消");
}
public void init()
{
f.setLayout(new FlowLayout());
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(b1);
f.add(b2);
f.add(l3);
f.setSize(200,200);
f.setLocation(400,350);
f.setVisible(true);
b1.addMouseListener(new MyListener());
b2.addMouseListener(new MyListener());
f.addMouseListener(new MyListener());
}
class MyListener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
if(e.getSource()==b1)
{
textName=t1.getText();
textPassword=t2.getText();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:dsStudent");
Statement st=con.createStatement();
String sql="select * from admin where name='"+textName+"' and password='"+textPassword+"'";
rs=st.executeQuery(sql);
if(rs.next())
{
System.out.println("验证成功");
//System.out.println(sql);
info.setEnabled(true);
datab.setEnabled(true);
Login.this.dispose();
}
else
{
showMessage();
}
rs.close();
con.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
else if(e.getSource()==b2)
{
Login.this.dispose();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询