Java连接MySQL数据库

1、首先添加下载。mysql-connector-java-3.1.14-bin.jar,把这个文件复制到 /WEB-INF文件夹下面,然后添加到项目中。

2、新建Conf.Properties文件:

#驱动
Driver=com.mysql.jdbc.Driver
#URL test为数据库名
Url=jdbc:mysql://localhost:3306/test
#用户名
user=root
#密码
password=123456

3、读取配置文件,连接数据库,数据库操作类

public class DBHelper {
    // 驱动
    private String driver;
    // 地址
    private String Url;
    // 用户名
    private String Name;
    // 密码
    private String Password;
    // Connection对象
    Connection conn;
    // Statement对象
    Statement state;
    // 查询的结果集
    ResultSet DataResult;

    DBHelper() { 
        // 读取文件 下面三种方式都可以 
        // path=this.getClass().getResource("/").getPath()+"conf.properties";
        String path = this.getClass().getClassLoader().getResource("conf.properties").getFile();
        // URL url=this.getClass().getResource("/");
        // 解码
        path = URLDecoder.decode(path);
        Properties proper = new Properties();
        try {
            proper.load(new FileInputStream(path));
        } catch (FileNotFoundException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        this.driver = proper.getProperty("Driver");      // 驱动
        this.Url = proper.getProperty("Url");              // URL
        this.Name = proper.getProperty("user");             // user
        this.Password = proper.getProperty("password");  // password
    }

    public void GetCon() {
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(this.Url, this.Name, this.Password);
            state = conn.createStatement();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    /*
     *  关闭连接
     */
    public void Close() {
         if(conn!=null){
             try {
                conn.close();
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
         }
         if(state!=null){
             try {
                state.close();
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
         }
    }
} 
  //通过Servlet获取conf.properties文件
    private void test3() throws IOException, FileNotFoundException {
            String path = this.getServletContext().getRealPath("/WEB-INF/classes/c.properties");
            Properties pro = new Properties();
            pro.load(new FileInputStream(path));
            
            System.out.println(pro.get("key"));
        }

下图为数据配置文件所在的路径:

页面:

<table>
        <tr>
            <td>id</td>
            <td>编号</td>
            <td>姓名</td>
            <td>性别</td>
            <td>生日</td>
            <td>地址</td>
        </tr>
        <%
            /* 
            ArrayList list=(ArrayList)request.getAttribute("Data");
            int ii=list.size();
            for(int i=0;i<list.size();i++){
                EmployeeModel model=(EmployeeModel)list.get(i);
                out.write("<tr>");
                out.write("<td>" + model.getId() + "</td>");
                out.write("<td>" + model.getEmpCode() + "</td>");
                out.write("<td>" + model.getImpName() + "</td>");
                out.write("<td>" + model.getEmpAddress() + "</td>");  
                out.write("<tr>");
            } */
        %> 
        <C:forEach var="list" items="${Data}">
            <tr>
                <td>${list.id}</td>
                <td>${list.impName}</td>
                <td>${list.empCode}</td> 
                <td>
                    <C:if test="${empSex==0}">男</C:if> 
                    <C:if test="${empSex==1}">女</C:if> 
                </td>  
                <td>
                    <fmt:formatDate value="${list.empBirthday}" pattern="yyyy-MM-dd"/>   //格式化时间
                </td>
                <td>${list.empAddress}</td>
                <td>${list.memo}</td>
            </tr>
        </C:forEach>

    </table>

查询出来数据:

原文地址:https://www.cnblogs.com/wwj1992/p/6121688.html