Swing+数据库连接

 数据库类型:SQL Server2000

切记,不要忘了加驱动包!

DB.java

Code:
  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.SQLException;  
  4.   
  5. public class DB {  
  6.       
  7.     private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=UserInfo";  
  8.                                 //数据库路径:服务器所在的计算机为localhost:1433;  
  9.                                 //数据库名称:UserInfo  
  10.     private static final String UserName = "xsy";    //用户名称  
  11.     private static final String PassWord = "xsy123";  //用户密码  
  12.     private static Connection conn = null;  
  13.       
  14.     public static Connection getConnection(){  
  15.         try {  
  16.             Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");  //加载驱动程序  
  17.             conn = DriverManager.getConnection(URL, UserName, PassWord);    //创建数据库连接  
  18.         } catch (SQLException e) {  
  19.             e.printStackTrace();  
  20.         } catch (ClassNotFoundException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.         return conn;  
  24.     }  
  25. }  

test1.java

Code:
  1. import java.awt.Color;  
  2. import java.awt.Container;  
  3. import java.awt.event.ActionEvent;  
  4. import java.awt.event.ActionListener;  
  5. import java.sql.Connection;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.sql.Statement;  
  9. import javax.swing.BorderFactory;  
  10. import javax.swing.JButton;  
  11. import javax.swing.JFrame;  
  12. import javax.swing.JLabel;  
  13. import javax.swing.JPanel;  
  14. import javax.swing.JTextField;  
  15. import javax.swing.border.TitledBorder;  
  16.   
  17. public class test1 extends JFrame implements ActionListener{  
  18.   
  19.     public static void main(String[] args) {  
  20.         test1 t1 = new test1();  
  21.         t1.setVisible(true);       //设置窗体可见  
  22.   
  23.     }  
  24.       
  25.     JButton button1;  
  26.     JButton button2;  
  27.     JTextField textField1;  
  28.     JTextField textField2;  
  29.     JTextField textField3;  
  30.     public test1(){  
  31.         super();  
  32.         this.setTitle("查询基本信息");  
  33.         this.setBounds(200,200,350,260);   //setSize(int width,int hight);setBounds(x,y,width,hight);  
  34.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  35.         Container p = getContentPane();  
  36.         p.setLayout(null);  
  37.           
  38.         JLabel label1 = new JLabel("姓名:");  
  39.         label1.setBounds(80,10,40,20);  
  40.         p.add(label1);  
  41.           
  42.         textField1 = new JTextField();  
  43.         textField1.setBounds(130,8,120,20);  
  44.         p.add(textField1);  
  45.           
  46.         button1 = new JButton("查询");  
  47.         button1.setBounds(100,40,60,20);  
  48.         button1.addActionListener(this);  //添加鼠标响应事件  
  49.         p.add(button1);  
  50.           
  51.         button2 = new JButton("重置");  
  52.         button2.setBounds(180,40,60,20);  
  53.         button2.addActionListener(this);  
  54.         p.add(button2);  
  55.           
  56.         JPanel resultPanel = new JPanel();  
  57.         resultPanel.setBounds(40,90,260,100);  
  58.           
  59.         //定义指定颜色,指定标题的边框  
  60.         TitledBorder tb = new TitledBorder(BorderFactory.createLineBorder(new Color(255,0,0)),"查询结果");  
  61.         tb.setTitleColor(Color.blue);//设置标题颜色  
  62.           
  63.         resultPanel.setBorder(tb);    //将标题边框添加到面板中  
  64.         //resultPanel.setBackground(Color.yellow);  
  65.         resultPanel.setLayout(null);  
  66.         JLabel label2 = new JLabel("姓名:");  
  67.         label2.setBounds(80,110,40,20);  
  68.         p.add(label2);  
  69.         textField2 = new JTextField();  
  70.         textField2.setBounds(130,108,120,20);  
  71.         p.add(textField2);  
  72.         JLabel label3 = new JLabel("密码");  
  73.         label3.setBounds(80,140,40,20);  
  74.         p.add(label3);  
  75.         textField3 = new JTextField();  
  76.         textField3.setBounds(130,140,120,20);  
  77.         p.add(textField3);  
  78.           
  79.         p.add(resultPanel);  
  80.     }  
  81.       
  82.     public void actionPerformed(ActionEvent e){  
  83.         JButton jb = (JButton)e.getSource();  
  84.         if(jb == button1){  
  85.             String name = textField1.getText();  
  86.             String password = search(name);  
  87.             textField2.setText(name);  
  88.             textField3.setText(password);     
  89.         }  
  90.         if(jb == button2){  
  91.             textField1.setText("");  
  92.         }  
  93.           
  94.     }  
  95.     public String search(String name){  
  96.         String password="";  
  97.         try {  
  98.             Connection conn = DB.getConnection();  
  99.             Statement stmt = conn.createStatement();  
  100.             String sql = "select UserPwd from User_info where UserName='"+name+"'";  
  101.             ResultSet rs = stmt.executeQuery(sql);  
  102.             while(rs.next()){  
  103.                 password = rs.getString(1);  
  104.             }  
  105.             stmt.close();  
  106.             conn.close();  
  107.         } catch (SQLException e) {  
  108.             e.printStackTrace();  
  109.         }  
  110.         return password;  
  111.     }  
  112. }  

 

你可能感兴趣的:(Swing+数据库连接)