JDBC-Java连接数据库

package JDBC;

import java.sql.*;
/**
 *@author g0rez
 *@data 2021-08-07
 * 1.注册驱动(告诉Java程序,即将连接的是哪个品牌的数据库)
 * 2.获取连接(表示JVM的进程和数据库进程之间的通道打开了,这属于进程之间的通信,使用完后记得关闭通道)。
 * 3.获取数据库操作对象(专门执行sql语句的对象)
 * 4.执行SQL语句(DQL,DML…)
 * 5.处理查询结果集 (只有当第四步执行的是select语句的时候,才有本步)
 * 6.释放资源(使用完资源后一定要关闭资源,Java和数据库之间属于进程间的通信,开启之后一定要记得关闭)
 */
public class JDBCTest01 {
    public static void main(String[] args) {
        Connection con=null;
        Statement stm = null;
        ResultSet rs=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_student","root","root");
            stm = con.createStatement();
            String sql = "select id,name,info from s_class";
            rs=  stm.executeQuery(sql);
            while (rs.next()){
                System.out.print(rs.getString(1)+" ");
                System.out.print(rs.getString(2)+" ");
                System.out.print(rs.getString(3));
                System.out.println();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 解决SQL注入问题

package JDBC;

import java.sql.*;
/*
* 解决sql注入问题
* 只要用户提供的信息 不参与sql语句的编译过程,问题就解决了
* 即使用户提供的信息中含有sql语句的关键字,但是没有参与编译,不起作用
* 要想用户信息不参与sql语句的编译,那么必须使用java.sql.PreparedStatement
* PreparedStatement接口继承了java.sql.PreparedStatement
* PreparedStatement是属于预编译的数据库操作对象。
* PreparedStatement的原理是:预先对sql语句的框架进行编译,然后再给sql语句传"值"
* 解决SQL注入问题的关键是什么?
* 用户提供的信息中即使含有sql语句的关键字 ,但是这些关键字并没有参与编译,不起作用。
*
* 对比PreparedStatement与Statement
* 1.Statement存在sql注入问题,PreparedStatement解决了sql注入问题
* 2.Statement是编译依次执行一次,PreparedStatement是编译一次,可以执行n次,效率略高
* 3.PreparedStatement会在编译阶段做类型安全检查
* 综上所述:大部分时候使用PreparedStatement   有些情况用statement,如需要输入desc(降序) asc(升序)
* */
public class 防止SQL注入 {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps =null;
        ResultSet rs =null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_student","root","root");
           // SQl语句的框子,其中一个?代表一个占位符,一个?将来接收一个值。(占位符不可以用单引号括起来)
            String sql = "select id,name,info  from s_class where id=?";
            ps = con.prepareStatement(sql);
            ps.setInt(1,2);
            rs = ps.executeQuery();
            while (rs.next()){
                System.out.print(rs.getString(1)+" ");
                System.out.print(rs.getString(2)+" ");
                System.out.println(rs.getString(3));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

 

posted @ 2021-08-07 18:49  guoyuxin3  阅读(41)  评论(0)    收藏  举报