【课堂笔记】javaweb的查询代码

javaweb的增删改查

实验准备

  1. 建立数据库sxy
  2. 在sxy的数据库建立表user
  3. 创建JavaEE项目

实现代码

代码架构:
请添加图片描述

数据库的建立,并填入数据。(填入数据略)

请添加图片描述


com.ym.demo -> DButils

package com.ym.demo;

import java.sql.*;
import java.util.ResourceBundle;

import static java.lang.Class.forName;

public class DButils {
    private static String  driverClass;
    private static String  url;
    private static String  username;
    private static String  password;

    static {
        ResourceBundle rb = ResourceBundle.getBundle("dbinfo");
        driverClass = rb.getString("driverClass");
        url = rb.getString("url");
        username = rb.getString("username");
        password = rb.getString("password");
        try {
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection() throws Exception {
        return DriverManager.getConnection(url, username, password);
    }

    public static void closeALL(ResultSet rs, Statement stmt, Connection conjn){
        if (rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        rs = null;

        if (stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        stmt = null;

        if (conjn != null){
            try {
                conjn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        conjn = null;
    }
}

com.ym.demo -> testCRUD

public class testCRUD {
    public void testSelect(){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<User> list = new ArrayList<>();
        try {
            conn = DButils.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select * from user");
            while ( rs.next() ) {
                User u = new User();
                u.setId(rs.getInt(1));
                u.setName(rs.getString(2));
                u.setPassword(rs.getString(3));
                list.add(u);
            }
            for (User user : list) {
                System.out.printf("user");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            DButils.closeALL(rs, stmt, conn);
        }


    }
}

com.ym.demo -> User

public class User {

    private int id;
    private String name;
    private String password;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p><marquee>13456780</marquee></p>
</body>
</html>

dbinfo.properties

driverClass = com.mysql.cj.jdbc.Driver
url = jdbc:mysql:///sxy
username = root
password = 123456

注意事项

  1. jdk8链接数据库用 cj.jdbc ; jdk7用jdbc
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值