JDBC:将员工表(Employee)中的员工姓名(empName)逐个显示在一个JComboBox中

dao类

/**
 *  开发一个JDBC小程序
 *  将Motel168客房管理系统员工表(Employee)中的
 *  员工姓名(empName)逐个显示在一个JComboBox中
 */
package com.Test.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.Test.Util.DBUtil;
import com.Test.dto.UsernameDTO;

public class UsernameDAO {

public List findAllUsers() {
    Connection conn = null;
    PreparedStatement statement = null;
    ResultSet rs = null;
    try {
        conn = DBUtil.getConnection();
        String sql = "select * from Employee";
        statement = conn.prepareStatement(sql);//Wrong
        rs = statement.executeQuery();

        List users = new ArrayList();

        while(rs.next()) {
            UsernameDTO userdto = new UsernameDTO();
            String name = rs.getString("empName");
            userdto.setEmpName(name);
            users.add(userdto);
        }

        return users;
    }
    catch(Exception e) {
        e.printStackTrace();
        return null;
    }
    finally {
        try {
            rs.close();//update no need
            statement.close();
            conn.close();
        }
        catch(Exception e1) {
            e1.printStackTrace();
        }
    }
  }
}

dto类

package com.Test.dto;

public class UsernameDTO {
    private String empName;

    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
}

DBUtil类

package com.Test.Util;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBUtil {
    public static Connection getConnection(){
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=HappyDB;user=sa;password=123456");
            return conn;
        }
        catch (Exception e) {

        }
        return null;
    }
}

UI类

/**
 *  开发一个JDBC小程序
 *  将Motel168客房管理系统员工表(Employee)中的员工姓名(empName)逐个显示在一个JComboBox中
 */
package com.Test.view;


import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.util.List;

import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;

import com.Test.dao.UsernameDAO;
import com.Test.dto.UsernameDTO;

public class UsernameDisplay extends JFrame{
//  private JFrame jf;
    private JComboBox cmb;


    //构造方法
    public UsernameDisplay() {
        this.setLayout(new FlowLayout());
        Container c = this.getContentPane();

        cmb = new JComboBox();
        c.add(cmb);

        UsernameDAO userdao = new UsernameDAO();

        List userDtoList = userdao.findAllUsers();

        for(int i = 0; i < userDtoList.size(); i++){
            UsernameDTO userdto = userDtoList.get(i);
            cmb.addItem(userdto.getEmpName());
        }


        this.setTitle("NameDisplay");
        this.setSize(300, 200);
        this.setLocationRelativeTo(null);
        int w = Toolkit.getDefaultToolkit().getScreenSize().width;
        int h = Toolkit.getDefaultToolkit().getScreenSize().height;
        this.setLocation(w/2-200, h/2-200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

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

}

JDBC:将员工表(Employee)中的员工姓名(empName)逐个显示在一个JComboBox中_第1张图片

你可能感兴趣的:(java)