JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。
本文使用的时MySQL数据库进行连接。
所用数据库表结构如下:
方式一:(基本使用方式)
步骤:
- 注册驱动
- 建立连接
- 创建Statement / PreparedStatement
- 执行查询,得到结果集
- 关闭资源
具体代码如下:
import java.sql.*;
/**
* JDBC使用
*/
public class JdbcDemo1 {
public static void main(String[] args){
String url = "jdbc:mysql://localhost:3306/king";
String user = "root";
String password = "123456";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.建立连接
connection = DriverManager.getConnection(url, user, password);
//3.创建statement
statement = connection.createStatement();
//4.执行查询 得到结果集
String sql = "select * from student";
resultSet = statement.executeQuery(sql);
//对结果集进行遍历
while(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println(id +" "+ name +" "+ age);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//5.关闭资源
try{
if(resultSet != null){
resultSet.close();
}
}catch (SQLException s){
s.printStackTrace();
}finally{
resultSet = null;
}
try{
if(statement != null){
statement.close();
}
}catch (SQLException s){
s.printStackTrace();
}finally{
statement = null;
}
try{
if(connection != null){
connection.close();
}
}catch (SQLException s){
s.printStackTrace();
}finally{
connection = null;
}
}
}
}
方式二:(配置文件+工具类实现)
流程与方法一大体相同,区别在于通过配置文件降低依赖关系,并抽取出工具类,供后面开发使用。
具体内容如下:
(1)配置文件:
(2)工具类:在工具类的静态代码块部分读取配置文件,然后注册驱动。
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUitls {
static String driverClass = null;
static String url = null;
static String user = null;
static String password = null;
static {
try{
Properties properties = new Properties();
InputStream in = JdbcUitls.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(in);
driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
}catch (Exception e){
e.printStackTrace();
}
}
//建立数据库连接
public static Connection getConnection(){
Connection connection = null;
try{
//1.注册驱动
Class.forName(driverClass);
//2.建立连接
connection = DriverManager.getConnection(url, user, password);
}catch (Exception e){
e.printStackTrace();
}
return connection;
}
//关闭资源
public static void closeResource(ResultSet resultSet, Statement statement, Connection connection){
closeResultSet(resultSet);
closeStatement(statement);
closeConnection(connection);
}
private static void closeResultSet(ResultSet resultSet){
try{
if(resultSet != null){
resultSet.close();
}
}catch (SQLException s){
s.printStackTrace();
}finally{
resultSet = null;
}
}
private static void closeStatement(Statement statement){
try{
if(statement != null){
statement.close();
}
}catch (SQLException s){
s.printStackTrace();
}finally{
statement = null;
}
}
private static void closeConnection(Connection connection){
try{
if(connection != null){
connection.close();
}
}catch (SQLException s){
s.printStackTrace();
}finally{
connection = null;
}
}
}
测试代码如下:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class JdbcDemoTest {
public static void main(String[] args){
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//建立连接
connection = JdbcUitls.getConnection();
//创建statemnet
statement = connection.createStatement();
//执行查询 得到结果集
String sql = "select * from student";
resultSet = statement.executeQuery(sql);
//对结果集进行遍历
while(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println(id +" "+ name +" "+ age);
}
}catch (Exception e){
e.printStackTrace();
}finally {
JdbcUitls.closeResource(resultSet, statement, connection);
}
}
}
*个人学习过程中的记录,如有问题欢迎批评指正,共同进步。