数据库
首页 > 数据库> > sqlHelper 类型的编写

sqlHelper 类型的编写

作者:互联网

在进行jdbc程序编写的时候,因为对数据库操作很多,

如果访问数据库很频繁,Connection不要设置成static

当编辑jdbc.properties文件时,window最后一栏preference里面找到

 

防止SQL注入:

package com.sql;

import java.sql.*;

public class Demo2 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;// 预处理对象
        ResultSet rs = null;
        try {
            // 1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("建立连接成功");
            // 2.建立连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hello", "root", "123456");
            conn.setAutoCommit(false);
            System.out.println(conn);
            // 3.创建ps,代表预编译对象
            ps = conn.prepareStatement("select* from student where id=? and name=?");
            // ps可以给?赋值
            ps.setInt(1, 6);
            ps.setString(2, "王志");
            // 4.执行
            rs = ps.executeQuery();

            while (rs.next()) {
                System.out.println(rs.getInt("id") + " " + rs.getString("name"));
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            // 如果sql语句中任何语句出错,可以整体回滚
            try {
                conn.rollback();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
                conn = null;
            }

            if (rs != null) {
                try {
                    ps.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                }
                rs = null;
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
                ps = null;
            }
        }

    }
}
View Code

 

sqlHelper:

package com.sql;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

import javax.management.RuntimeErrorException;

public class sqlHelper {
    // 定义需要的变量
    private static Connection ct = null;
    private static Statement sm = null;
    private PreparedStatement ps = null;
    private static ResultSet rs = null;
    // 连接数据库的参数
    private static String url = "";
    private static String user = "";
    private static String driver = "";
    private static String password = "";
    private static Properties pp = null;
    private static FileInputStream fis = null;
    // 加载一次驱动
    static {
        try {
            // 读取配置信息
            pp = new Properties();
            fis = new FileInputStream("dbinfo.properties");
            pp.load(fis);
            url = pp.getProperty("url");
            driver = pp.getProperty("driver");
            password = pp.getProperty("password");
            user = pp.getProperty("user");

            Class.forName(url);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            fis = null;
        }
    }

    // 得到连接
    public static Connection getConnection() {

        try {
            ct = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ct;
    }

    // 自动获取
    // 先写一个修改update/insert/delete语句
    // sql格式:update 表名字 set 字段名=? where 字段=?
    // parameters 应该是("abc",23);
    public void excuteUpdate(String sql, String[] parameters) {
        // 创建一个ps
        try {
            ct = getConnection();
            ps = ct.prepareStatement(sql);
            // 给?赋值
            if (parameters != null) {
                for (int i = 0; i < parameters.length; i++) {
                    ps.setString(i + 1, parameters[i]);

                }
            }
            // 执行
            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();// 开发阶段
            // 抛出异常,抛出运行异常可以给调用该函数的函数一个选择,可以处理也可以放弃处理
            throw new RuntimeException(e.getMessage());

            // TODO: handle exception
        } finally {
            close(rs, ps, ct);
        }
    }

    // 如果有多个则要考虑事务
    public static void excuteUpdate2(String sql[], String[][] parameters) {

    }

    public static void close(ResultSet rs, Statement ps, Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
            conn = null;
        }

        if (rs != null) {
            try {
                ps.close();
            } catch (Exception e2) {
                // TODO: handle exception
            }
            rs = null;
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
            ps = null;
        }
    }
}
View Code

dbinfo.properties

#这是我的mysql配置
url=jdbc:mysql://localhost:3306/hello
user=root
driver=com.mysql.jdbc.Driver
password=123456
View Code

 

标签:ps,sqlHelper,try,catch,static,类型,编写,null,conn
来源: https://www.cnblogs.com/helloworld2019/p/10802697.html