JDBC
Jdbc快速入门
1.1、Jdbc概念
1.2、模拟jdbc
//1.定义接口
package com.jdbc;
public interface JdbcInterface {
public Object getConnection();
public void crud();
public void close();
}
//2.定义接口实现类
package com.jdbc;
public class JdbcInterfaceImpl implements JdbcInterface{
@Override
public Object getConnection() {
System.out.println("连接mysql数据库");
return null;
}
@Override
public void crud() {
System.out.println("对mysql进行增删改查");
}
@Override
public void close() {
System.out.println("关闭连接");
}
}
//3.测试mysql
package com.jdbc;
public class TestMysql {
public static void main(String[] args) {
JdbcInterface jdbcInterface = new JdbcInterfaceImpl();
jdbcInterface.getConnection();
jdbcInterface.crud();
jdbcInterface.close();
//建立连接
//增删改查
//关闭连接
}
}
1.3、jdbc快速入门
package com.jdbc;
import com.mysql.cj.jdbc.Driver;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class Jdbc01 {
public static void main(String[] args) throws SQLException {
//注册驱动
Driver driver = new Driver();
//创建连接
String url = "jdbc:mysql://127.0.0.1:3306/db1" +
"?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","root");
//建立连接
Connection conn = driver.connect(url,properties);
//执行sql
String sql = "insert into actor values(null,'Make','男','1970-11-11','110')";
Statement statement = conn.createStatement();
int i = statement.executeUpdate(sql);
System.out.println(i > 0 ? "成功" : "失败");
//关闭资源
statement.close();
conn.close();
}
}
1.4、数据库连接方式
1.4.1、连接方式二
@Test
public void connect02() {
try {
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url = "jdbc:mysql://127.0.0.1:3306/db1" +
"?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","root");
Connection connect = driver.connect(url, properties);
String sql = "select * from actor where id = 1";
Statement statement = connect.createStatement();
statement.executeQuery(sql);
statement.close();
connect.close();
System.out.println(connect);
} catch (Exception e) {
e.printStackTrace();
}
}
}
1.4.2、连接方式三
@Test
public void connect03() {
try {
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url = "jdbc:mysql://127.0.0.1:3306/db1" +
"?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
String user = "root";
String password = "root";
DriverManager.registerDriver(driver);
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "select * from actor";
Statement statement = conn.createStatement();
statement.executeQuery(sql);
statement.close();
conn.close();
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
1.4.3、连接方式四
@Test
public void connect04() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/db1" +
"?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "select * from actor";
Statement statement = conn.createStatement();
statement.executeQuery(sql);
statement.close();
conn.close();
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
1.4.4、连接方式五
@Test
public void connect05() {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("src\\mysql.properties"));
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);
} catch (IOException | SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
1.4.4.1、配置文件
url=jdbc:mysql://localhost:3306/db4?serverTimezone=UTC
user=root
password=root
driver=com.mysql.cj.Driver
1.5、ResultSet结果集
package com.jdbc;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class ResultSet_ {
public static void main(String[] args) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("src\\mysql.properties"));
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);
Statement statement = connection.createState