定义
·· JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序,同时,JDBC也是个商标名。
写一个实例类
package domain;
import java.util.Date;
public class User {
private int id;
private String username;
private String password;
private String email;
private Date birthday;
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setEmail(String email) {
this.email = email;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getEmail() {
return email;
}
public Date getBirthday() {
return birthday;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", birthday=" + birthday +
'}';
}
}
定义一个工具类用于得到connection对象
package com.itheima.util;
import java.sql.*;
import java.util.ResourceBundle;
public class DBUtils {
/**
* 得到jdbc中connection的方法
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
private static String driverClass;
private static String url;
private static String username;
private static String password;
static{
ResourceBundle dbinfo = ResourceBundle.getBundle("dbinfo");
driverClass = dbinfo.getString(driverClass);
url = dbinfo.getString(url);
username = dbinfo.getString(username);
password = dbinfo.getString(password);
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws ClassNotFoundException, SQLException {
return DriverManager.getConnection(url, username, password);
}
public static void closeAll(ResultSet resultSet, Statement statement,Connection connection){
if(resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
resultSet = null;
}
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
statement = null;
}
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
}
}
}
设置配置文件
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql:///day12
username=root
password=abc
写一个测试类检查
package com.itheima.crud;
import com.itheima.util.DBUtils;
import domain.User;
import org.junit.Test;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class TestCRUD {
@Test
public void testSelect(){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = DBUtils.getConnection();
String sql = "select * from users";
preparedStatement = connection.prepareStatement(sql);//使用预编译对象
resultSet = preparedStatement.executeQuery();
List<User> list = new ArrayList<>();
while (resultSet.next()){
User user = new User();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
user.setEmail(resultSet.getString(4));
user.setBirthday(resultSet.getDate(5));
list.add(user);
}
for (User user : list) {
System.out.println(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtils.closeAll(resultSet, preparedStatement, connection);
}
}
得到输出结果打印在控制台
User{id=1, username=’tom’, password=’123’, email=’[email protected]’, birthday=1992-08-02}