带你入门JDBC

什么是JDBC

​ JDBC(java database connectivity): sun公司为了简化和统一java连接数据库,定义的一套规范。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RMWZkf23-1605785528379)(C:\Users\Linkin Park\AppData\Roaming\Typora\typora-user-images\image-20201119175143112.png)]

JDBC快速入门

步骤

  1. 创建Java工程, 导入驱动 import java.sql jar包
  2. 注册JDBC驱动程序,和数据库建立一个连接通道,Class.forName()
  3. 获得连接对象,使用DriverManager.getConnection()方法创建一个连接对象。
  4. 创建Statement或PreparedStatement对象,该对象可以执行SQL语句
  5. 如果有结果集,则遍历结果集,使用ResultSet.getXX()方法,通过while循环遍历
  6. 清理环境资源,后创建的先关闭

代码实现

//工具类
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class utils {
    private static String driverName;//驱动程序
    private static String username;//用户名
    private static String password;//密码
    private static String url;//连接服务器


    static {
        InputStream is = null;
        try {
            //创建Properties对象,用于加载properties文件
            Properties properties = new Properties();

            //将配置文件转换成流
            ClassLoader classLoader = utils.class.getClassLoader();
            is = classLoader.getResourceAsStream("//类路径");

            //properties对象加载流
            properties.load(is);

            //从properties对象中获取值
            driverName = properties.getProperty("jdbc.driver");
            username = properties.getProperty("jdbc.username");
            password = properties.getProperty("jdbc.password");
            url = properties.getProperty("jdbc.url");

            //建立连接通道
            Class.forName(driverName);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    //获得连接对象
    public static Connection getConnection() throws Exception {
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
    }

    //关闭资源
    public static void close(Statement stm, Connection conn) throws Exception {
        close(null,stm,conn);
    }
	//重载方法
    public static void close(ResultSet rst, Statement stm, Connection conn) throws Exception {
        if (rst != null){
            rst.close();
        }

        if(stm != null){
            stm.close();
        }

        if (conn != null) {
            conn.close();
        }
    }
}
//测试类(简单用户登入)
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class login {
    public static void main(String[] args) throws Exception {
        //控制台提示
        System.out.println("请输入用户名:");
        //接收输入的用户名
        Scanner scanner = new Scanner(System.in);
        String username = scanner.nextLine();
        //控制台提示
        System.out.println("请输入密码:");
        //接收输入的密码
        String password = scanner.nextLine();

        //创建连接对象,让Java和MySQL连接
        Connection conn = utils.getConnection();

        //预编译sql语句
        String sql = "select * from emp where username = ? and password = ? ";

        //预编译
        PreparedStatement pst = conn.prepareStatement(sql);
        
        //设置占位符参数
        pst.setString(1,username);
        pst.setString(2,password);

        //执行SQL语句
        ResultSet rst = pst.executeQuery();

        //遍历结果集,有值就会得到true
        if (rst.next()) {
            //结果集中有数据,那么就是登录成功
            System.out.println("登录成功...");
        }else {
            //登录失败
            System.out.println("登录失败...");
        }

        //关闭资源
        utils.close(rst,pst,conn);


    }
}

JDBC事务的处理

try{
	connection.setAutoCommit(false); //开启事务
	...操作数据库
	connection.commit(); //提交事务
}catch(Exection e){
	connection.rollback(); //回滚事务
}finally{
	...释放资源
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值