JDBC

JDBC

  • JDBC是什么
  • JDBC什么时候用
  • JDBC怎么用
  • 实例

JDBC是什么

  • . 全称:Java Database Connectivity,是用于在Java语言编程中与数据库连接的API。
  • . 作用:
    1. 连接到数据库
    2. 创建SQL语句
    3. 在数据库中执行SQL语句
    4. 返回执行结果

JDBC什么时候用

  • 前、后端的交互中会使用到JDBC

怎么用JDBC

关键字:DriverManager
  • 作用:用于管理数据库驱动列表,使用通信子协议将来自Java的连接请求与适当的数据库驱动程序相匹配
  • 使用:DriverManager(url,username,password)
    • url:地址,具有特殊写法,比如“jdbc:mysql//localhost:3306/table_name"
    • username:登录数据库的用户名
    • password:登录数据库的密码
关键字:Connection
  • 作用:该类具有用于联系数据库的所有方法
  • 使用:Connect conn=DriverManager(url,username,password);
关键字:Statement
  • 作用:将SQL语句提交到数据库
  • 使用:
    • 创建:Statement state=conn.createStatement();
    • 提交:提交分为两种方法:
      • state.executeUpdate:用于增、删、改,修改成功返回收到影响的行数,修改失败则返回0
      • state.executeQuery:用于查找,返回一个ResultSet类型
关键字:PreparedStatement
  • 作用:自动排除注入攻击;将从控制台获取的数据注入SQL语句
  • 使用:
    • 创建:PreparedStatement ps=conn.preparedStatement();
    • 注入:根据数据类型分类注入,并输入要注入的字段在SQL语句中的位置与要注入的内容
      • ps.setInt(1,int i); //在select语句的第一处条件字段注入一个int值
      • ps.setString(2,String str); //在select语句的第二处条件字段注入一个字符串
关键字:ResultSet
  • 作用:作为迭代器,遍历state对象
  • 前提:state对象成功执行用于查询的SQL语句
  • 使用:
    • 创建:ResultSet rs=state.executeQuery(select语句);
    • 遍历:rs.next()
      • 移动指针,若指向的数据不为null,则返回true;否则,返回false
    • 获取:根据数据类型分类获取,并输入要获取的字段在返回表中的位置(从1开始),如果成功返回收到影响的行数
      • rs.getInt(ColumnIndex); //获取整数型
      • rs.getString(ColumnIndex); //获取字符型
关键字:SQLException
  • 作用:用于处理联系数据库过程中发生的任何异常

实例

实例1:向数据库插入一条数据
//**********************************向数据库新增一条数据***********************************
import java.sql.*;
import java.util.Scanner;

public class JDBC1 {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        Connection conn=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");//JDK1.7及以下版本必须写,1.8开始可以不写
            String url="jdbc:mysql://localhost:3306/cn_xien?useSSL=true&characterEncoding=utf8";
            String username="root";
            String password="0611";
            conn=DriverManager.getConnection(url,username,password);//连接数据库驱动

            String update="insert into user(uid,username,password) values(6,'孙狗','111111')";
            Statement state=conn.createStatement();//创建state对象
            int i=state.executeUpdate(update);//提交SQL语句,并获取返回值
            System.out.println(i);//打印返回值,根据返回值判断是否成功
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(conn!=null){
                    conn.close();	//关闭连接
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
实例2:将从控制台获取的数据注入SQL语句
import java.sql.*;
import java.util.Scanner;

public class JDBC2 {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        Connection conn=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");

            String url="jdbc:mysql://localhost:3306/cn_xien?useSSL=true&characterEncoding=utf8";
            String username="root";
            String password="0611";
            conn= DriverManager.getConnection(url,username,password);
			//从控制台获取数据
            System.out.println("请输入ID:");
            int uid=scan.nextInt();
            System.out.println("请输入密码:");
            String upwd=scan.next();

            String select="update user set password=? where uid=?";//"?"表示要注入的数据位置
            PreparedStatement ps=conn.prepareStatement(select);
            ps.setString(1,upwd);	//向”password“字段注入upwd数据
            ps.setInt(2,uid); //向"uid"字段注入数据
            int line = ps.executeUpdate();
            System.out.println(line);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                conn.close();	//关闭连接
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

  • 26
    点赞
  • 110
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值