java JDBC编程学习笔记

JDBC 简介

什么是JDBC

JDBC(Java DataBase Connectivity)就是Java数据库连接,顾名思义,就是用Java语言来操作数据库。原来我们操作数据库是在数据库的可视化界面使用SQL语句来操作数据库,JDBC是用Java语言向数据库发送SQL语句。
关于JDBC的原理和其他相关的介绍推荐这位博主的文章:https://blog.csdn.net/weixin_41547486/article/details/80710567

JDBC的简单写法

我总结了一些写法:

笔记

1.建立一个连接connection
		例:con= DriverManager.getConnection(url,user,password);
			url的写法:url="jdbc:mysql://主机地址:端口号/数据库名称?统一字符集&字符编码&加密连接";
			例:url="jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf8&useSSL=true";
	2.获得类的对象
		Class.forName("com.mysql.cj.jdbc.Driver");//这里可能会有些问题,如果有出现问题换成com.mysql.jdbc.Driver
	3.写SQL语句 例:String sql="sql语句";
	4.创建一个声明:statement st;
		st=con.createStatement();
	5.创建一个结果集:ResultSet re;
		re=st.executeUpdate(sql);//插入、更新、删除都用executeUpdate,查询用executeQuery;

做完笔记就要上手写一写,这样才会熟练!

简单例子

package mysql;

import java.sql.*;
public class Query{
    public static void main(String[] args) {
        Connection con=null;
        Statement st=null;
        ResultSet re=null;
        String url="jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf8&useSSL=true";
        String sql="select * from selectCourse where studentName like '庄%'";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con= DriverManager.getConnection(url,"root","root");
            st=con.createStatement();
            re=st.executeQuery(sql);
            while (re.next()){
                String s1=re.getString(1);
                String s2=re.getString(2);
                String s3=re.getString(3);
                String s4=re.getString(4);
                System.out.print(s1);
                System.out.print("   "+s2);
                System.out.print("   "+s3);
                System.out.println("    "+s4);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            if (re!=null){
                try {
                    re.close();
                } catch (SQLException throwable) {
                    throwable.printStackTrace();
                }
            }
            if (st!=null){
                try {
                    st.close();
                } catch (SQLException throwable) {
                    throwable.printStackTrace();
                }
            }
            if (con!=null){
                try {
                    con.close();
                } catch (SQLException throwable) {
                    throwable.printStackTrace();
                }
            }
        }
    }
}

这样子就是一个简单的例子:通过java向指定的数据库查询数据。如果我们要写一个两个这种代码还可以这样写,但如果我们要通过java写大量这样子的增删改查代码的话,有一些语句都是重复的就会很浪费人力物力,所以我们考虑把那些重复不变的代码封装成一个工具类,这样子写一次就行了。

JDBC封装

笔记

可以将一些相同的代码自己封装成一个工具类:
	把driver,url,username,password封装在properties;
		例:driver=com.mysql.cj.jdbc.Driver
			url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf8&useSSL=true
			username=root
			password=root
	然后建一个uitl类
		在类中通过流的方式获取properties里面的资源
			InputStream in =jdbcUtil.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);
		将资源连接和资源释放分别封装成两个方法
			1.getConnection()
			2.release(Connection conn, Statement st, ResultSet rs)
	测试类:
		conn= 工具类.资源连接方法();//获取数据库连接
        st= conn.createStatement();//获取SQL的执行对象
		String sql="SQL语句";
		st.executeUpdate(sql);//插入、更新、删除都用executeUpdate,查询用executeQuery;
		然后在finally调用资源释放方法jdbcUtil.release(conn,st,rs);

笔记做好,上手↓

封装的例子

Properties工具

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=root

util类

package studykuang.utils;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class jdbcUtil {
    private static String driver=null;
    private static String url=null;
    private static String username=null;
    private static String password=null;
    static {
        try {
            InputStream in = jdbcUtil.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 (SQLException throwable) {
                throwable.printStackTrace();
            }
        }
        if (st!=null){
            try {
                st.close();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        }

    }
}

main类

这个类需要改动的也只是SQL语句和执行条件

package studykuang;

import studykuang.utils.jdbcUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestInsert {
    public static void main(String[] args) {
        Connection conn=null;
        Statement st=null;
        ResultSet rs=null;
        try {
            conn= jdbcUtil.getConnection();//获取数据库连接
            st= conn.createStatement();//获取SQL的执行对象
            String sql="INSERT INTO student(studentNo,studentName,studentAge,Specialty)" +
                    "VALUES('1009','YJL',35,'basketball')";
            int i = st.executeUpdate(sql);
            if (i>0){
                System.out.println("数据插入成功!");
            }
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }finally {
            jdbcUtil.release(conn,st,rs);//如果rs没有返回值,填null也是OK
        }
    }

}

在例子中我提到com.mysql.cj.jdbc.Driver(com.mysql.jdbc.Driver)这个问题,这个跟我们所用的mysql-connector-java的版本有关。具体讲解推荐这篇文章:https://blog.csdn.net/superdangbo/article/details/78732700


本篇文章到这就结束了。每次在网上看视频,都会做一些笔记,如果各位有好的做笔记方式方法可以跟我交流交流,共同进步;如果这篇文章有可以优化的地方希望大佬指教指教。

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT小混子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值