Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3592127
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Java

2020-05-19 12:01:50

这是Java连接数据库最常用的方法,对注入的sql语句进行提前预设,固定格式,可以防止注入漏洞。

直接上代码:

执行增删改操作:

/*

* 防止sql注入漏洞(or/--sql关键字带来的漏洞)

* 最常用的jdbc接口Java连接数据库的方法。

* */

import java.sql.Connection;

import java.sql.PreparedStatement;

public class preparedStatementInsert {

    public static void main(String[] args) {

        Connection con = null;

        //定义准备声明与处理对象,规定sql语句的模式

        PreparedStatement preparedStatement = null;

        //jdbc连接工具集

        JDBCUtils jdbcUtils = new JDBCUtils();

        try{

            //DriverManager.getConnection(),传如url,user,password,连接数据库

           con = jdbcUtils.getConnection();

           //准备mysql语句,可以用?替代某些变动区域,
   tring sql =
String sql = "update student set spassword = '778832' where sid = 6"; String sql = "update student set spassword = ? where sid = ?";

            //预声明准备,传入sql语句

           preparedStatement = con.prepareStatement(sql);

           //设置与声明sql语句的?内容

           preparedStatement.setString(1,"778832");

           preparedStatement.setString(2,"5");

           //执行更新,返回作用的行int,查询语句返回resultSet结果集。

           int result = preparedStatement.executeUpdate();

           //如果作用的行大于0,说明操作成功。

//            System.out.println("result:"+result);  //result:1

           if(result>0){

               System.out.println("操作成功!");

           }else

               System.out.println("操作失败!");

        }catch (Exception e){

 

        }finally {

            //释放资源

            jdbcUtils.relResource(con,preparedStatement);

      

进行查询

/*

* 查询数据库jdbc基本方式

* */

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

public class preparedStatementSelect {

    public static void main(String[] args) {

        Connection con = null;

        //准备声明预处理对象

        PreparedStatement preparedStatement = null;

        //jdbc工具集,前面的文章有公布。

        JDBCUtils jdbcUtils = new JDBCUtils();

        try {

            //获得连接对象

            con = jdbcUtils.getConnection();

            //需要执行的sql语句,

//            String sql = "select * from student where sname='张三' and  spassword='123456'";

            String sql = "select * from student";

            //sql语句预处理,到下个文章中我们将演示sql注入漏洞的产生

            preparedStatement = con.prepareStatement(sql);

            //执行,返回结果集

            ResultSet resultSet = preparedStatement.executeQuery();

            //遍历结果集

            while(resultSet.next()){

                //还有通用方法,getObject(index or paramName)

                int id = resultSet.getInt("sid");

                String name = resultSet.getString("sname");

                String password = resultSet.getString("spassword");

                System.out.println(id+" "+name+" "+password);

            }

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            jdbcUtils.relResource(con,preparedStatement);

阅读(2315) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~