MySQL数据库-------statement对象和PreparedStatement对象

statement对象

  • jdbc中的statement对象用于向数据库发送SQL语句,可以通过这个对象向数据库发送增删改查语句完成对数据库的增删改查

    • Statement对象的executeUpdate方法,用于向数据库发送增、删、改的sql语句,方法执行完后返回一个整数(即sql语句导致了数据库几行数据发生了变化)
    • Statement.executeQuery方法用于向数据库发送查询语句,方法返回代表查询结果的ResultSet对象
  • 代码实现步骤

    1.提取工具类

    2.编写增删改的方法:executeUpdate

//添加一个文件夹
dirver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=123456
//创建一个工具类
package www.bh.c.utils;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcUtils {
    
    

    private static String driver=null;
    private static String url=null;
    private static String username=null;
    private static String password=null;

    static {
    
    
        try {
    
    
            InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties=new Properties();
            properties.load(in);
            driver=properties.getProperty("driver");
            url=properties.getProperty("url");
            username=properties.getProperty("username");
            password=properties.getProperty("password");
            //驱动只加载一次
            Class.forName(driver);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }

    //获取连接
    public static Connection getConnection() throws SQLException {
    
    
        return DriverManager.getConnection(url,username,password);
    }
    //释放资源
    public static void release(Connection conn, Statement st, ResultSet rs){
    
    
        if (rs!=null){
    
    
            try {
    
    
                rs.close();
            } catch (Exception e) {
    
    
                e.printStackTrace();
            }
        }
        if (st!=null){
    
    
            try {
    
    
                st.close();
            } catch (SQLException e) {
    
    
                e.printStackTrace();
            }
        }
        if (conn!=null){
    
    
            try {
    
    
                conn.close();
            } catch (SQLException e) {
    
    
                e.printStackTrace();
            }
        }
    }
}
//测试插入
package www.bh.c.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class InsertTest {
    
    
    public static void main(String[] args) {
    
    
        Connection conn=null;
        Statement st=null;
        ResultSet rs=null;

        try {
    
    
            conn=JdbcUtils.getConnection();//获取数据库连接
            st=conn.createStatement();//获取SQL的执行对象
            String sql="insert into jdbc_users(id,'name','psw') values(4,'D','123456')";
            int i=st.executeUpdate(sql);
            if (i>0){
    
    
                System.out.println("插入成功");
            }
        }catch (SQLException e) {
    
    
            e.printStackTrace();
        }finally {
    
    
            JdbcUtils.release(conn,st,rs);
        }
    }
}
//测试查询
package www.bh.c.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SelectTest {
    
    
    public static void main(String[] args) {
    
    
        Connection conn=null;
        Statement st=null;
        ResultSet rs=null;

        try {
    
    
            conn=JdbcUtils.getConnection();//获取数据库连接
            st=conn.createStatement();//获取SQL的执行对象
            String sql="select *from jdbc_users where id=1";
            rs=st.executeQuery(sql);//查询完毕会返回一个结果集
           while (rs.next()){
    
    
               System.out.println(rs.getString("name"));
           }
        }catch (SQLException e) {
    
    
            e.printStackTrace();
        }finally {
    
    
            JdbcUtils.release(conn,st,rs);
        }
    }
}
  • SQL注入的问题

    • sql存在漏洞,会被攻击导致数据泄露

    • 原因是sql会被拼接 or

      login("'or'1=1"," 'or' 1=1")
      

PreparedStatement对象

  • PreparedStatement可以防止 SQL注入,效率也更好
//测试更新
package www.bh.c.utils;
import java.sql.*;

public class UpdateTest2 {
    
    
    public static void main(String[] args) {
    
    
        Connection conn=null;
        PreparedStatement st=null;
        ResultSet rs=null;

        try {
    
    
            conn=JdbcUtils.getConnection();//获取数据库连接
            //区别:使用?占位符代替参数
            String sql="update jdbc_users set `name`=? where `id`=?)";
            st=conn.prepareStatement(sql);//预编译sql,先写sql,不执行
            //手动给参数赋值
            st.setString(1,"Jerry");
            st.setString(2,"1");
            //执行
            int i=st.executeUpdate();
            if (i>0){
    
    
                System.out.println("更新成功");
            }
        }catch (SQLException e) {
    
    
            e.printStackTrace();
        }finally {
    
    
            JdbcUtils.release(conn,st,rs);
        }
    }
}
//测试查询
package www.bh.c.utils;

import java.sql.*;

public class SelectTest2 {
    
    
    public static void main(String[] args) {
    
    
        Connection conn=null;
        PreparedStatement st=null;
        ResultSet rs=null;

        try {
    
    
            conn=JdbcUtils.getConnection();//获取数据库连接
            String sql="select *from jdbc_users where id=?";//编写sql
            st=conn.prepareStatement(sql);//预编译
            st.setInt(1,1);//传递参数
            rs=st.executeQuery();//执行
            while (rs.next()){
    
    
                System.out.println(rs.getString("name"));
            }
        }catch (
                SQLException e) {
    
    
            e.printStackTrace();
        }finally {
    
    
            JdbcUtils.release(conn,st,rs);
        }
    }
}
  • PreparedStatement防止SQL注入的本质:把传递进来的参数当做字符,假设其中存在了转义字符,比如 ’会被直接转义

猜你喜欢

转载自blog.csdn.net/insist_to_learn/article/details/112035100
今日推荐