數據庫類型:SQL Server2000
切記,不要忘了加驅動包!
DB.java
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class DB {
- private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=UserInfo";
- //數據庫路徑:服務器所在的計算機爲localhost:1433;
- //數據庫名稱:UserInfo
- private static final String UserName = "xsy"; //用戶名稱
- private static final String PassWord = "xsy123"; //用戶密碼
- private static Connection conn = null;
- public static Connection getConnection(){
- try {
- Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //加載驅動程序
- conn = DriverManager.getConnection(URL, UserName, PassWord); //創建數據庫連接
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return conn;
- }
- }
test1.java
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.border.TitledBorder;
- public class test1 extends JFrame implements ActionListener{
- public static void main(String[] args) {
- test1 t1 = new test1();
- t1.setVisible(true); //設置窗體可見
- }
- JButton button1;
- JButton button2;
- JTextField textField1;
- JTextField textField2;
- JTextField textField3;
- public test1(){
- super();
- this.setTitle("查詢基本信息");
- this.setBounds(200,200,350,260); //setSize(int width,int hight);setBounds(x,y,width,hight);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Container p = getContentPane();
- p.setLayout(null);
- JLabel label1 = new JLabel("姓名:");
- label1.setBounds(80,10,40,20);
- p.add(label1);
- textField1 = new JTextField();
- textField1.setBounds(130,8,120,20);
- p.add(textField1);
- button1 = new JButton("查詢");
- button1.setBounds(100,40,60,20);
- button1.addActionListener(this); //添加鼠標響應事件
- p.add(button1);
- button2 = new JButton("重置");
- button2.setBounds(180,40,60,20);
- button2.addActionListener(this);
- p.add(button2);
- JPanel resultPanel = new JPanel();
- resultPanel.setBounds(40,90,260,100);
- //定義指定顏色,指定標題的邊框
- TitledBorder tb = new TitledBorder(BorderFactory.createLineBorder(new Color(255,0,0)),"查詢結果");
- tb.setTitleColor(Color.blue);//設置標題顏色
- resultPanel.setBorder(tb); //將標題邊框添加到面板中
- //resultPanel.setBackground(Color.yellow);
- resultPanel.setLayout(null);
- JLabel label2 = new JLabel("姓名:");
- label2.setBounds(80,110,40,20);
- p.add(label2);
- textField2 = new JTextField();
- textField2.setBounds(130,108,120,20);
- p.add(textField2);
- JLabel label3 = new JLabel("密碼");
- label3.setBounds(80,140,40,20);
- p.add(label3);
- textField3 = new JTextField();
- textField3.setBounds(130,140,120,20);
- p.add(textField3);
- p.add(resultPanel);
- }
- public void actionPerformed(ActionEvent e){
- JButton jb = (JButton)e.getSource();
- if(jb == button1){
- String name = textField1.getText();
- String password = search(name);
- textField2.setText(name);
- textField3.setText(password);
- }
- if(jb == button2){
- textField1.setText("");
- }
- }
- public String search(String name){
- String password="";
- try {
- Connection conn = DB.getConnection();
- Statement stmt = conn.createStatement();
- String sql = "select UserPwd from User_info where UserName='"+name+"'";
- ResultSet rs = stmt.executeQuery(sql);
- while(rs.next()){
- password = rs.getString(1);
- }
- stmt.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return password;
- }
- }