JDBC连接与相关操作

JDBC

jdbc对数据库操作的步骤

首先要将jdbc连接数据库的jar包导入到项目中

 

然后右键这个JAR包,将他ADD到你需要的项目,所有的jar包单独导入都可以用这个方法

导入完成之后可以开始敲代码;

jdbc连接数据库的jar包可以到相应数据库的官网去下载,mysql就去mysql官网,orcal就去orcal官网,记得下载和你数据库匹配的版本;

如mysql5 和mysql8 使用上就有些许区别

  Class.forName("com.mysql.cj.jdbc.Driver");//mysql5.0版本不用.cj
//直接com.mysql.jdbc.Driver
        String url="jdbc:mysql://localhost:3306/test";
        String user="root";
        String pass="123456";
        Connection connection = DriverManager.getConnection(url, user, pass);

        String sql="delete from stu where id = ?";
        System.out.println(sql);
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,127);
        ps.execute();
        ps.close();
        connection.close();

 

1:注册驱动

 Class.forName("com.mysql.cj.jdbc.Driver");

2:创建数据库链接

String url="jdbc:mysql://localhost:3306/test";
        String user="root";
        String pass="123456";
        Connection connection = DriverManager.getConnection(url, user, pass);
​

3:创建Statement 类型或者子类型preparestatement

 String sql="delete from stu where id = ?";
PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,127);
 

4:执行sql

ps.execute();

5:处理结果

6:关闭资源

     
   ps.close();
        connection.close();

statement和PreparedStatement的区别

1:使用方法的区别

statement

 String sql="select * from user where username= '"+name+"' and password = '"+passw+"'";
//如果条件多的话字符的拼接很麻烦,必须在sql语句写完整,不能通过对象赋值,或其他赋值方式
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);

preparedStatement

Connection connection = DriverManager.getConnection(url, user, pass);
​
String sql="delete from stu where id = ?";
//preparedStatemend在sql语句中可以使用?占位符,避免了statement 拼接字符串的繁琐
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1,127);//索引,可以通过对象赋值给id等参数
ps.execute();
ps.close();
connection.close();

2:安全性

PrepareStatement的优点

  1. 避免字符串的拼接(通过占位符,再传递参数)

  2. 更加面向对象的编程方式

  3. 防止sql注入!

jdbc增删改操做

准备工作与关闭资源操作都相同;

建表

  
Class.forName("com.mysql.cj.jdbc.Driver");//注册驱动
        String url="jdbc:mysql://localhost:3306/test";
        String uname="root";
        String password="123456";
        Connection cont = DriverManager.getConnection(url, uname, password);//获取数据库
连接
      
  String sql="create table user (username varchar(20),password int)";
        System.out.println(sql);
        Statement statement = cont.createStatement();
        statement.execute(sql);//执行sql
        statement.close();
        cont.close();

插入数据

String sql="insert into user(username,password) values (?,?)";
        System.out.println(sql);
        PreparedStatement ps = cont.prepareStatement(sql);//预编译sql
        ps.setString(1,"lmr");
        ps.setString(2,"123456");
        ps.execute();

更新

 String sql="update user set username='李明' where username=?";
        System.out.println(sql);
        PreparedStatement ps=cont.prepareStatement(sql);
        ps.setString(1,"wang");
        ps.execute();

只有数据的查询方法比较特殊

statement 对象或者子对象,调用executeQuery()方法;

返回一个ResultSet 结果集:ResultSet对象保持一个光标指向其当前的数据行。 最初,光标位于第一行之前。 next方法将光标移动到下一行,并且由于在ResultSet对象中没有更多行时返回false ,因此可以在while循环中使用循环来遍历结果集

  String sql="select * from user";
PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet resultSet = ps.executeQuery();
        Collection<User> collection=new ArrayList<>();//通过集合来保存数据
        while(resultSet.next()){
            String name=resultSet.getString(1);//列索引,也可以传入列名 
            //通过列名来获取结果:resultSet.getString("name")
            String password=resultSet.getString(2);
            User u=new User(name,password);
            collection.add(u);
        }
        for (User user1 : collection) {
            System.out.println(user1);
        }
        ps.close();
        connection.close();

Sql注入

方式:通过expression1 or expression 2原理

如果expresssion1 表达式为True 那么 or之后的表达式短路,

例如:

输入用户名:"admin' or '1'='1",因为admin 判定为True 后面的表达式短路 输入密码:随意 可以登录成功 因为,生成的sql语句: select * from t_user where username = 'admin' or '1'='1' and password = '' 发生了“短路”。 称之为“sql注入”。 安全隐患!

防止sql注入:通过PrepareStatement

jdbc常见错误

1:sql语句条件拼接的时候发生错误

select * from user where username= '"+name+"' and password = '"+passw+"'";//符号错误,或者其他

2:关键字错误

delete-->delect

3:中英文输入错误

4:参数不匹配

最后附上相关的完整代码

package com.day3;


import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;

public class Ddl {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        login2("admin' or '1'='1","");


    }


    public static void create() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");//注册驱动
        String url="jdbc:mysql://localhost:3306/test";
        String uname="root";
        String password="123456";
        Connection cont = DriverManager.getConnection(url, uname, password);//获取数据库连接


        String sql="create table user (username varchar(20),password int)";
        System.out.println(sql);
        Statement statement = cont.createStatement();
        statement.execute(sql);//执行sql
        statement.close();
        cont.close();
    }

    public static void add() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");//注册驱动
        String url="jdbc:mysql://localhost:3306/test";
        String uname="root";
        String password="123456";
        Connection cont = DriverManager.getConnection(url, uname, password);//获取数据库连接

        String sql="insert into user(username,password) values (?,?)";
        System.out.println(sql);
        PreparedStatement ps = cont.prepareStatement(sql);//预编译sql
        ps.setString(1,"lmr");
        ps.setString(2,"123456");
        ps.execute();
        ps.close();
        cont.close();
    }


    public static void update() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");//注册驱动
        String url="jdbc:mysql://localhost:3306/test";
        String uname="root";
        String password="123456";
        Connection cont = DriverManager.getConnection(url, uname, password);//获取数据库连接

        String sql="update user set username='李明' where username=?";
        System.out.println(sql);
        PreparedStatement ps=cont.prepareStatement(sql);
        ps.setString(1,"wang");
        ps.execute();
        ps.close();
        cont.close();
    }

    public static void delete() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/test";
        String user="root";
        String pass="123456";
        Connection connection = DriverManager.getConnection(url, user, pass);

        String sql="delete from stu where id = ?";
        System.out.println(sql);
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,127);
        ps.execute();
        ps.close();
        connection.close();

    }

    public static void show() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/test";
        String user="root";
        String pass="123456";
        Connection connection = DriverManager.getConnection(url, user, pass);

        String sql="select * from user";
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet resultSet = ps.executeQuery();
        Collection<User> collection=new ArrayList<>();//通过集合来保存数据
        while(resultSet.next()){
            String name=resultSet.getString(1);//列索引,也可以传入列名
            String password=resultSet.getString(2);
            User u=new User(name,password);
            collection.add(u);
        }
        for (User user1 : collection) {
            System.out.println(user1);
        }
        ps.close();
        connection.close();
    }

    public static void login(String uname,String password) throws ClassNotFoundException, SQLException {//preparestatement可以避免sql注入
        // 此方法用于测试SQL注入,原理  expression or expression 表达式1如果正确,后面的表达式发生短路
        boolean flag=false;
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/test";
        String user="root";
        String pass="123456";
        Connection connection = DriverManager.getConnection(url, user, pass);

        String sql="select * from user where username=? and password=?";
        PreparedStatement ps=connection.prepareStatement(sql);
        ps.setString(1,uname);//传入的参数作为查询条件
        ps.setString(2,password);

        ResultSet resultSet = ps.executeQuery();
        if(resultSet.next()){
           flag=true;
        }
        if(flag){
            System.out.println("密码正确,登录成功");
        }else {
            System.out.println("密码或者用户名错误");
        }
        ps.close();
        connection.close();

    }

    public  static void login2(String name,String passw )throws ClassNotFoundException, SQLException{
        // 此方法用于测试SQL注入,原理  expression or expression 表达式1如果正确,后面的表达式发生短路
        boolean flag=false;
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/test";
        String user="root";
        String pass="123456";
        Connection connection = DriverManager.getConnection(url, user, pass);

        String sql="select * from user where username= '"+name+"' and password = '"+passw+"'";
        System.out.println(sql);
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        if(resultSet.next()){
            flag=true;
        }
        if(flag){
            System.out.println("密码正确,登录成功");
        }else {
            System.out.println("密码或者用户名错误");
        }
        statement.close();
        connection.close();

       // select * from user where username= 'admin' or '1'='1' and password = ''
       // 密码正确,登录成功
        //statement 安全较低,会被sql注入成功,前面为true or之后的表达式短路
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值