JDBC基础二

1.配置文件:dbinfo.properties

1
2
3
4
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test
username=root
password=root

2.DBUtils.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.mf.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ResourceBundle;
public class DBUtils {
    private static String driverClass;
    private static String url;
    private static String username;
    private static String password;
    static{
        //此对象是用于加载properties文件数据的
        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) {
            e.printStackTrace();
        }
    }  
    //得到连接的方法
    public static Connection getConnection() throws Exception{
        return DriverManager.getConnection(url, username, password);
    }
    //关闭资源的方法
    public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
        //关闭资源
        if(rs!=null){
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
}

3.解决sql注入问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class DoLogin {
     
    /**
     * 根据用户名和密码查询用户对象信息
     * @param name
     * @param pwd
     * @return u
     */
    public User findUser(String name,String pwd){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        User u = null;
        try {
            conn = DBUtils.getConnection();//得到连接对象Connection
            String sql ="SELECT * FROM users WHERE NAME=? AND PASSWORD=?";
            stmt = conn.prepareStatement(sql);//得到执行sql语句的对象Statement
            //给?赋值
            stmt.setString(1, name);
             
            rs = stmt.executeQuery();//执行sql语句
            if(rs.next()){
                u = new User();
                u.setId(rs.getInt(1));
                u.setName(rs.getString(2));
                u.setPassword(rs.getString(3));
                u.setEmail(rs.getString(4));
                u.setBirthday(rs.getDate(5));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBUtils.closeAll(rs, stmt, conn);
        }
        return u;
    }

  

 https://www.cnblogs.com/fzz9/p/8970210.html

posted @   ~沐风  阅读(180)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示