DriverManager:驱动管理对象 功能: (1)注册驱动:告诉程序该使用那种数据库
代码中常使用:Class.forName("com.mysql.cj.jdbc.Driver");
会被加载进内存,在源码中可发现com.mysql.cj.jdbc.Driver类中存在静态代码块
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) //
package com.mysql.cj.jdbc;
import java.sql.DriverManager; import java.sql.SQLException;
public class Driver extends NonRegisteringDriver implements java.sql.Driver { public Driver() throws SQLException { }
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
}
***在MySQL5之后可以不注册驱动,因为在MySQLjar包中有文件替你注册驱动
(2)获取数据库连接
Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.44.1:3306/mydb1","root","200211"); 调用DriverManager中的静态方法getConnection(可以直接用类名调用)返回Connection类型对象
***如果连接的是本机MySQL服务器,则MySQL服务器默认为端口是3306,则IP地址和端口号均可以省略。url可以简写为:"jdbc:mysql:///数据库名“
Connection:数据库连接对象 与特定的数据库的链接(接口)
功能: (1)获取执行SQL对象Statement
①普通的执行SQL对象:
statement createStatement()
②预编译SQL的执行SQL对象:(防止SQL注入)
prepare preparestatement(SQL)
(2)管理事务:
在MySQL中:
开启事务:
BEGIN
提交事务:
COMMIT
回滚事务
ROLLBACK
MySQL默认自动提交事务
JDBC事务管理:
Connection接口中定义三个对应方法
开启事务:setAutoCommit(boolean autoCommit):为TRUE为自动提交事务,FALSE为手动提交事务,且开启事务
提交事务:commit()
回滚事务:rollback()
代码实现:
package jdbc;
import java.sql.*;
public class jdbcconnection { public static void main(String[] args) throws SQLException {
String url="jdbc:mysql:///School?useSSL=false"; String username="root";
String password="123112"; Connection connection1 = DriverManager.getConnection(url, username, password);
String sql="update student set sdept='cs' where sno='08003'"; String sql1="update student set sdept='cs' where sno='08002'";
Statement statement = connection1.createStatement(); try {
connection1.setAutoCommit(false);//开启事务 int count= statement.executeUpdate(sql);
// int i1=3/0; int i = statement.executeUpdate(sql1);
System.out.println(count); System.out.println(i);
//提交事务 ---到底了说明结束了 connection1.commit();
} catch (Exception e) { //回滚事务
connection1.rollback(); e.printStackTrace();
} finally { statement.close();
connection1.close(); }
}
`` }
Statement:执行SQL的对象
Resultset:结果集对象
PreparedStatement:执行SQL对象
jdbc-Statement
statement作用: 执行SQL语句 int executeUpdate(sql) 执行DML,DDL语句 返回值(1)DML(对表中数据进行增删改)语句影响行数 (2)DDL(对表操作语句)语句执行后**,执行成功也可能返回0**(不能用是否结果为0来判断是否成功)(不报异常即可) ResultSet executeQuery(sql) 执行DQL语句 返回值:ResultSet结果集对象 ResultSet:
函数介绍: next()函数 用到函数:bool类型 next()(光标默认是指向数据的上一行) (1)将光标从当前位置向前移动一行 (2)判断当前行是否为有效行 返回值:TRUE 有效行 FALSE 当前行无数据 getxxx()函数 getxx(参数)--函数--获取数据(打印数据) xxxgetxx(参数)xxx是类型 如 int String 参数:int 列的编号(编号从1开始) String 列名称(可以写编号) 如 String getString(参数)
代码实现:
package jdbc; import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager; import java.sql.SQLException;
import java.sql.Statement; /执行dml语句/
public class jdbcstatement { @Test
public void testDML() throws Exception { String url="jdbc:mysql:///School?useSSL=false";
String username="root"; String password="12312";
Connection connection1 = DriverManager.getConnection(url, username, password);
String sql="update student set sdept='cs' where sno='08003'"; String sql1="update student set sdept='cs' where sno='08002'";
Statement statement = connection1.createStatement(); try {
connection1.setAutoCommit(false);//开启事务 int count= statement.executeUpdate(sql);
if(count>0) {
System.out.println("修改成功"); }
else {
System.out.println("修改失败"); }
// int i1=3/0; int i = statement.executeUpdate(sql1);
System.out.println(count); System.out.println(i);
//提交事务 ---到底了说明结束了 connection1.commit();
} catch (Exception e) { //回滚事务
connection1.rollback(); e.printStackTrace();
} finally { statement.close();
connection1.close(); }
}
}