分不多..谢谢各位大大了..我是这样做的..首先在数据库中做了个表..用来存放用户名跟密码!数据用的SQL server2005
我数据类型都是用的varchar型然后用两个参数接收了登陆界面文本框中输入的用户名跟密码..再把数据库中存的用户名密码读出来..比较..但是无论怎么也匹配不了..以下是代码:接受用户名密码
password = text.getText();
username = text_1.getText();
Login.login(username,password);
比较方法 public static void login(String _username,String _password){
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");//装载驱动
//建立连接
Connection conn = DriverManager.getConnection(
"jdbc:jtds:sqlserver://localhost:1433/HRM", "sa", "sa");
PreparedStatement pstmt = conn
.prepareStatement("select * from manager where haha='aaa'");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
username=rs.getString("haha");
password=rs.getString("password");
}
conn.close();
if(_username.equals(null) || _password.equals(null)){
JOptionPane.showMessageDialog(null,"账号或者密码为空");
}
if(_username.equals(username)||_password.equals(password)){
Display display = Display.getDefault();
FunctionInterface shell = new FunctionInterface(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
else
JOptionPane.showMessageDialog(null,"账号或者密码错误"); } catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我数据类型都是用的varchar型然后用两个参数接收了登陆界面文本框中输入的用户名跟密码..再把数据库中存的用户名密码读出来..比较..但是无论怎么也匹配不了..以下是代码:接受用户名密码
password = text.getText();
username = text_1.getText();
Login.login(username,password);
比较方法 public static void login(String _username,String _password){
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");//装载驱动
//建立连接
Connection conn = DriverManager.getConnection(
"jdbc:jtds:sqlserver://localhost:1433/HRM", "sa", "sa");
PreparedStatement pstmt = conn
.prepareStatement("select * from manager where haha='aaa'");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
username=rs.getString("haha");
password=rs.getString("password");
}
conn.close();
if(_username.equals(null) || _password.equals(null)){
JOptionPane.showMessageDialog(null,"账号或者密码为空");
}
if(_username.equals(username)||_password.equals(password)){
Display display = Display.getDefault();
FunctionInterface shell = new FunctionInterface(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
else
JOptionPane.showMessageDialog(null,"账号或者密码错误"); } catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我提议:你将数据库得到的username和password打印出来.
另外._username和_password也打印出来..
就可以看到是那一块取值错误了..
0就是说名用户名或密码错