初始JDBC
一、下载JDBC的驱动
链接地址:https://mvnrepository.com/artifact/mysql/mysql-connector-java/
二、创建一个项目
创建lib文件夹,把驱动拉进去,右键lib文件夹把驱动添加到library (Add As Library)
三、写代码
// 我的第一个JDBC程序public class jdbcDemo {public static void main(String[] args) throws ClassNotFoundException, SQLException {// 1.加载驱动Class.forName("com.mysql.jdbc.Driver"); // 固定写法,加载驱动// 2.用户信息和url/*useUnicode=true 支持中文characterEncoding=utf8 设置中文编码utf8useSSL=true 使用安全的连接*/// useUnicode=true 支持中文&characterEncoding=utf8 设置中文编码utf8&useSSL=true 使用安全的连接String url = "jdbc:mysql://localhost:3306/shuai?useUnicode=true&characterEncoding=utf8&useSSL=true";String username = "root";String password = "shuai";// 3.连接成功,数据库对象Connection connection = DriverManager.getConnection(url,username,password);// 4.执行SQL的对象 Statement创建执行sql的对象Statement statement = connection.createStatement();// 5.执行SQL的对象 去 执行SQL,可能存在结果,查看返回结果String sql = "SELECT * FROM userInfo";ResultSet resultSet = statement.executeQuery(sql); // 返回的结果集,结果集中封装了我们全部的查询出来的结果while (resultSet.next()){System.out.println("id=" + resultSet.getObject("id"));System.out.println("username=" + resultSet.getObject("username"));System.out.println("password=" + resultSet.getObject("password"));System.out.println("email=" + resultSet.getObject("email"));System.out.println("birthday=" + resultSet.getObject("birthday"));System.out.println("=============================");}// 6.释放连接resultSet.close();statement.close();connection.close();}}
步骤总结:
- 加载驱动
- 连接数据库 DriverManager
- 获得执行sql的对象 statement
- 获得返回的结果集
- 释放连接
DriverManager
// DriverManager.registerDriver(new com.mysql.jdbc.Driver()); (不推荐使用,Driver原码中已经注册过了)Class.forName("com.mysql.jdbc.Driver"); // 固定写法,加载驱动// connection 代表数据库connection.rollback(); // 事务回滚connection.commit(); // 事务提交connection.setAutoCommit(); // 数据库设置自动提交
URL
String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true";// useUnicode=true 支持中文// characterEncoding=utf8 设置中文编码utf8// useSSL=true 使用安全的连接
Statement 执行SQL 的对象 prepareStatement 执行SQL 的对象
String sql = "SELECT * FROM userInfo"; // 编写SQLstatement.executeQuery(); // 查询操作返回 ResultSetstatement.execute(); // 执行任何SQLstatement.executeUpdate(); // 更新、插入、删除。都是用这个,返回一个受影响的行数
ResultSet 查询的结果集:封装了所有的查询结果
jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查的查询语句即可
resultSet.getObject(); // 在不知列类型的情况下使用// 如果知道列的类型就使用指定的类型resultSet.getInt();resultSet.getString();resultSet.getFloat();resultSet.getDate();
遍历,指针
resultSet.beforeFirst(); // 移动到最前面resultSet.afterLast(); // 移动到最后面resultSet.next(); // 移动到下一个数据resultSet.previous(); // 移动到前一行resultSet.absolute(row); // 移动到指定行
释放资源
// 6.释放连接(用完关掉,耗资源)resultSet.close();statement.close();connection.close();
JDBC封装成工具类
一、创建db.properties文件
这个文件的作用是数据库的配置文件,里面保存了驱动类、url、用户名、密码和sql语言的信息。
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/shuai?useUnicode=true&characterEncoding=utf8&useSSL=trueusername=rootpassword=shuai
二、创建工具包utils
public class jdbcUtils {// 数据源提高作用域private static String driver = null;private static String url = null;private static String username = null;private static String password = null;static {try {// 通过反射拿到对应的资源流InputStream in = jdbcUtils.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");// 1.驱动只用加载一次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 throwables) {throwables.printStackTrace();}}if (st!=null){try {st.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (conn!=null){try {conn.close();} catch (SQLException throwables) {throwables.printStackTrace();}}}}
三、代码展示
import com.shuai2.utils.jdbcUtils; // 导入工具包public static void main(String[] args) {Connection conn = null;Statement st = null;ResultSet rs = null;try{conn = jdbcUtils.getConnection(); // 获取数据库连接st = conn.createStatement(); // 获得SQL的执行对象String sql = "INSERT INTO userinfo(username,password,email,birthday) VALUES('小王','133','133@qq.com','2001-02-26');";int i = st.executeUpdate(sql);if (i>0){System.out.println("插入成功");}} catch (SQLException throwables) {throwables.printStackTrace();} finally {jdbcUtils.release(conn,st,rs);}}
