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. }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章