我在数据库中设置好了访问数据库用户的名称和密码,然后我想通过在验证窗口输入的用户名和密码来验证连接!然后弹出一个窗口!我搞了一天的脑子也没有想出来!这个改怎么写啊??
我用的是mysql+myEcilpse做的登陆窗口
我是一名初学者 本程序通过jdk1.4.1编译的
package StudentProject;
import java.sql.*;
//DBManager 该类为公共类连接数据库的
public class DBManager {
private Connection con;
private ResultSet rs;
public DBManager()
{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
con=DriverManager.getConnection("jdbc:mysql://localhost/mysql?useUnicode=true&characterEncoding=gb2312");
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet excuteQerty(String sql){
Statement st;
try {
st = (Statement) con.createStatement();
rs=st.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public Statement addUser(String sql){
Statement st = null;
try {
st = (Statement) con.createStatement();
st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return st;
}
}
package StudentProject;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import Example921.*;
public class DengLu extends JFrame implements ActionListener {
private JLabel jLabel1,jLabel2;
private JTextField txt1;
private JPasswordField txt2;
private JButton b1,b2;
public DengLu(){
Container contentPane=getContentPane();
contentPane.setLayout(new GridLayout(3,2));
jLabel1=new JLabel("用户:");
jLabel2=new JLabel("密码:");
txt1=new JTextField(20);
txt2=new JPasswordField(20);
b1=new JButton("登录");
b2=new JButton("退出");
contentPane.add(jLabel1);
contentPane.add(txt1);
contentPane.add(jLabel2);
contentPane.add(txt2);
contentPane.add(b1);
contentPane.add(b2);
this.setUndecorated(true);
this.setResizable(false);
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
this.setTitle("欢迎您进入管理信息系统");
this.setBounds(300,300,350,170);
this.setVisible(true);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
if(txt1.getText().trim().equals("")){
JOptionPane.showMessageDialog(null,"用户名不能为空!");
}
else if(txt2.getText().trim().equals("")){
JOptionPane.showMessageDialog(null,"密码不能为空!");
}else {
try{
dengLu();
}
catch(Exception e1){
e1.toString();
}
}
}
if(e.getSource()==b2){
int n=JOptionPane.showConfirmDialog(this,"是否退出","您是否想退出?",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION){
System.exit(0);
}
else{
this.setVisible(true);
}
}
}
// TODO Auto-generated method stub
private void dengLu() {
String sql="select * from login where UserName='"+txt1.getText().trim()+"'and Password='"+txt2.getText().trim()+"'";
DBManager db=new DBManager();
ResultSet rs=db.excuteQerty(sql);
try {
if(rs.next()){
this.setVisible(false);
MainWindow mainWindow=new MainWindow();
mainWindow.setVisible(true);
}
else{
JOptionPane.showMessageDialog(null,"您的用户不存在!");
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[]){
new DengLu();
}
}