基本介绍
JDBC为访问不同的数据库提供了统一的接口,为使用者屏蔽了细节问题。 Java程序员使用JDBC,可以连接任何提供了JDBC驱动程序的数据库系统,从而完成对数据库的各种操作。 JDBC 原理图 JDBC 的好处? JDBC是Java提供一套用于数据库操作的接口APl,Java程序员只需要面向这套接口编程即可 。不同的数据库厂商,需要针对这套接口,提供不同实现。 JDBC API是一系列的接口,它统一和规范了应用程序与数据库的连接、执行SQL语句,并到得到返回结果等各类操作,相关类和接口在 java.sql
与javax.sql
包中
第一个 JDBC 程序
编写步骤
注册驱动 – 加载 Driver
类 获取连接 – 得到 Connection
执行增删改查 – 发送 SQL
给 mysql
执行 释放资源 – 关闭相关连接
package Test ;
import com. mysql. cj. jdbc. Driver ;
import java. sql. Connection ;
import java. sql. SQLException ;
import java. sql. Statement ;
import java. util. Properties ;
public class Test5 {
public static void main ( String [ ] args) throws SQLException {
Driver driver = new Driver ( ) ;
String url = "jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai" ;
Properties properties = new Properties ( ) ;
properties. setProperty ( "user" , "root" ) ;
properties. setProperty ( "password" , "root" ) ;
Connection conn = driver. connect ( url, properties) ;
String sql = "insert into actor values(null, 'Vodka', '男', '1977-10-10', '222')" ;
Statement st = conn. createStatement ( ) ;
Integer rows = st. executeUpdate ( sql) ;
System . out. println ( rows > 0 ? "执行 SQL 成功!" : "执行 SQL 失败!" ) ;
st. close ( ) ;
conn. close ( ) ;
}
}
获取数据库连接的 5 种方式
package Test ;
import com. mysql. cj. jdbc. Driver ;
import org. junit. Test ;
import java. io. FileInputStream ;
import java. io. IOException ;
import java. sql. Connection ;
import java. sql. DriverManager ;
import java. sql. SQLException ;
import java. util. Properties ;
public class Test6 {
@Test
public void connection1 ( ) throws SQLException {
Driver driver = new com. mysql. cj. jdbc. Driver( ) ;
String url = "jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai" ;
Properties properties = new Properties ( ) ;
properties. setProperty ( "user" , "root" ) ;
properties. setProperty ( "password" , "root" ) ;
Connection conn = driver. connect ( url, properties) ;
System . out. println ( conn) ;
}
@Test
public void connection2 ( ) throws ClassNotFoundException , InstantiationException , IllegalAccessException , SQLException {
Class < ? > aClass = Class . forName ( "com.mysql.cj.jdbc.Driver" ) ;
Driver driver = ( Driver ) aClass. newInstance ( ) ;
String url = "jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai" ;
Properties properties = new Properties ( ) ;
properties. setProperty ( "user" , "root" ) ;
properties. setProperty ( "password" , "root" ) ;
Connection conn = driver. connect ( url, properties) ;
System . out. println ( conn) ;
}
@Test
public void connection3 ( ) throws ClassNotFoundException , InstantiationException , IllegalAccessException , SQLException {
Class < ? > aClass = Class . forName ( "com.mysql.cj.jdbc.Driver" ) ;
Driver driver = ( Driver ) aClass. newInstance ( ) ;
String url = "jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai" ;
String user = "root" ;
String password = "root" ;
DriverManager . registerDriver ( driver) ;
Connection connection = DriverManager . getConnection ( url, user, password) ;
System . out. println ( connection) ;
}
@Test
public void connection4 ( ) throws ClassNotFoundException , SQLException {
Class . forName ( "com.mysql.cj.jdbc.Driver" ) ;
String url = "jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai" ;
String user = "root" ;
String password = "root" ;
Connection connection = DriverManager . getConnection ( url, user, password) ;
System . out. println ( connection) ;
}
@Test
public void connection5 ( ) throws IOException , ClassNotFoundException , SQLException {
Properties properties = new Properties ( ) ;
properties. load ( new FileInputStream ( "src\\mysql.properties" ) ) ;
String url = properties. getProperty ( "url" ) ;
String driver = properties. getProperty ( "driver" ) ;
String user = properties. getProperty ( "user" ) ;
String password = properties. getProperty ( "password" ) ;
Class . forName ( driver) ;
Connection conn = DriverManager . getConnection ( url, user, password) ;
System . out. println ( conn) ;
}
}
ResultSet
package Test ;
import java. io. FileInputStream ;
import java. io. IOException ;
import java. sql. * ;
import java. util. Properties ;
public class Test7 {
public static void main ( String [ ] args) throws IOException , ClassNotFoundException , SQLException {
Properties properties = new Properties ( ) ;
properties. load ( new FileInputStream ( "src\\mysql.properties" ) ) ;
String url = properties. getProperty ( "url" ) ;
String driver = properties. getProperty ( "driver" ) ;
String user = properties. getProperty ( "user" ) ;
String password = properties. getProperty ( "password" ) ;
Class . forName ( driver) ;
Connection conn = DriverManager . getConnection ( url, user, password) ;
String sql = "select id, name, sex, borndate, phone from actor" ;
Statement st = conn. createStatement ( ) ;
ResultSet resultSet = st. executeQuery ( sql) ;
while ( resultSet. next ( ) ) {
Integer id = resultSet. getInt ( 1 ) ;
String name = resultSet. getString ( 2 ) ;
String sex = resultSet. getString ( 3 ) ;
String borndate = resultSet. getString ( 3 ) ;
String phone = resultSet. getString ( 4 ) ;
System . out. println ( id + '\t' + name + '\t' + sex + '\t' + borndate + '\t' + phone) ;
}
resultSet. close ( ) ;
st. close ( ) ;
conn. close ( ) ;
}
}