当前位置: 代码迷 >> Java相关 >> 求一个模式对话框验证的源代码.
  详细解决方案

求一个模式对话框验证的源代码.

热度:257   发布时间:2006-09-23 16:54:38.0
求一个模式对话框验证的源代码.

我想作一个员工管理系统,在进入主模块前想进行一个验证,就是先弹出一个模式对话框,让用户来输入用户名和密码,然后到数据库验证一下,,就是这个验证环节老是出问题,请各位谁有关于验证的源代码给我一个看看.

搜索更多相关的解决方案: 源代码  对话框  模式  验证  

----------------解决方案--------------------------------------------------------

DBManager.java 为连接mysql数据库的例子 为公共的类
package studentproject;
import java.sql.*;
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 = con.createStatement();
rs=st.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return rs;
}
}
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();
}
}
}
// TODO Auto-generated method stub
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);
}
}
}
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);
}
else{
JOptionPane.showMessageDialog(null,"您的用户不存在!");

}
} catch (SQLException e) {

e.printStackTrace();
}


}
public static void main(String args[]){
new DengLu();
}

}

只是一个登录的窗口连接数据库的例子
我用的是myeclipse通过验证的


----------------解决方案--------------------------------------------------------

谢谢了 ,可是我主要是想看看模式对话框是怎么和主窗口相联系的.?


----------------解决方案--------------------------------------------------------
myeclipse,有没有连接好啊 ,?
----------------解决方案--------------------------------------------------------
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
上面代码中的,是什么意思?
----------------解决方案--------------------------------------------------------
setWindowDecorationStyle(int windowDecorationStyle)
Sets the type of Window decorations (such as borders, widgets for closing a Window, title ...) the JRootPane should provide.
认真看一下帮助文挡吧
----------------解决方案--------------------------------------------------------
  相关解决方案