JDBC连接Mysql 8.0.12版本的几个注意事项 - 简书
1、基础
1、JDBC是为了访问不同的数据库提供了统一的接口,为使用者屏蔽了细节问题
2、java程序员使用JDBC可以连接任何提供了JDBC驱动程序的数据库系统,从而完成对数据库的各种操作
3、java提供一种接口(java厂商制定规范),让mysql实现接口(一堆类)->jar[驱动],oracle实现接口(很多类)->jar[驱动],db2实现接口(很多类)->jar[驱动],sql server实现接口(类)->jar驱动,java程序中调用接口中的方法即可,要想使用对应的数据库,必须先引入对应驱动的jar包
4、JDBC是java提供的一套用于数据库操作的接口API,java程序员只需要面向这套接口编程即可。不同数据库厂商,需要针对这套接口,提供不同实现
5、JDBC API是一系列接口,主要在java.sql和javax.sql包中
2、JDBC快速入门
1、注册驱动-加载Driver类
2、获取连接-得到Connection
3、执行增删改查-发送SQL给mysql执行
4、释放资源-关闭相关连接
//首先导入mysql的驱动包,add to library
public class Jdbc02 {
public static void main(String[] args) throws SQLException {
//1-注册驱动-加载Driver类
Driver driver = new Driver();
//2-获取连接-得到Connection, 其实是一个socket连接
String url = "jdbc:mysql://localhost:3306/dbtest1?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","zHaNg123");
Connection connect = driver.connect(url, properties);
//3-执行增删改查-发送SQL给mysql执行
String sql = "delete from actor where id = 1";
Statement statement = connect.createStatement();
int rows = statement.executeUpdate(sql);
System.out.println(rows > 0 ? "成功" : "失败");
//4-释放资源-关闭相关连接
statement.close();
connect.close();
}
}
3、获取数据库连接的5种方式
2中为第一种方式
public class Jdbc03 {
//方式1
@Test
public void connect01() throws SQLException {
Driver driver = new Driver();
String url = "jdbc:mysql://localhost:3306/dbtest1?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "zHaNg123");
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
//方式2
@Test
public void connect02() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
//使用反射加载Driver类,动态加载,更加的灵活,减少依赖性
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url = "jdbc:mysql://localhost:3306/dbtest1?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "zHaNg123");
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
//方式3 使用DriverManager 替代 Driver进行统一管理
@Test
public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
//使用类反射加载Driver
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url = "jdbc:mysql://localhost:3306/dbtest1?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
String user = "root";
String password = "zHaNg123";
DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
//方式4:使用Class.forName 自动完成注册驱动,简化代码
//这种方式获取连接是开发中使用最多的
@Test
public void connect04() throws ClassNotFoundException, SQLException {
//使用反射加载了Driver类
//在加载Driver类时,完成注册
//Driver内部有一个静态加载块,完成Driver的注册
//注册Driver的工作已经完成
//mysql驱动5.16以后可以无需显式调用class.forName()注册驱动而是自动调用驱动
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/dbtest1?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
String user = "root";
String password = "zHaNg123";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
@Test
//方式5:使用配置文件,连接数据库更灵活
//实际开发使用第五种方式!!!
public void connect05() throws IOException, ClassNotFoundException, SQLException {
//通过properites对象获取配置文件的信息
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properites"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
}
4、ResultSet【结果集】
1、表示数据库结果集的数据表,通常由查询语句生成
2、刚开始指向表头,调用resultSet.next()方法会将指针下移,指向下一行,如果下一行不存在,返回false
public class ResultSet {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
//通过properites对象获取配置文件的信息
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properites"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
//1注册驱动
Class.forName(driver);
//2获取连接
Connection connection = DriverManager.getConnection(url, user, password);
//3sql查询
Statement statement = connection.createStatement();
String sql = "select id,name,sex,borndate,phone from actor";
java.sql.ResultSet resultSet = statement.executeQuery(sql);
//5循环取出数据 使用while取出数据
System.out.println(resultSet);
while(resultSet.next()) //让光标向后移动,如果没有更多行,则返回false
{
int id = resultSet.getInt(1); //获取该行的第一列
String name = resultSet.getString(2); //获取第二列
String sex = resultSet.getString(3);
Date date = resultSet.getDate(4);
String phone = resultSet.getString(5);
System.out.println(id + "\t" + name + "\t" + sex + "\t" + date + "\t" + phone);
}
}
}
5、Statement(为什么用PreparedStatement不用Statement)
Statement存在SQL注入,注入非法语句段或者命令,恶意攻击数据库
实际开发用PreparedStatement