什么是JDBC
JDBC(Java Database Connectivity),即Java數(shù)據(jù)庫連接,是一種用于執(zhí)行SQL語句的Java API,可以為多種關(guān)系數(shù)據(jù)庫提供同一訪問,它由一組用Java語言編寫的類和接口組成。JDBC提供了一種基準(zhǔn),根據(jù)這種基準(zhǔn)可以構(gòu)建更高級的工具和接口,使數(shù)據(jù)庫開發(fā)人員能夠編寫數(shù)據(jù)庫應(yīng)用程序。總而言之,JDBC做了三件事:
1、與數(shù)據(jù)庫建立連接
2、發(fā)送操作數(shù)據(jù)庫的語句
3、處理結(jié)果
JDBC簡單示例
下面的代碼演示了如何利用JDBC從數(shù)據(jù)庫中查詢?nèi)舾蓷l符合要求的數(shù)據(jù)出來,使用的數(shù)據(jù)庫是MySql。
1、建立一個數(shù)據(jù)庫和一張表,我的習(xí)慣是在CLASSPATH底下建立一個.sql的文件用于存放sql語句
create database school;
use school;
create table student
(
studentId int primary key auto_increment not null,
studentName varchar(10) not null,
studentAge int,
studentPhone varchar(15)
)
insert into student values(null,‘Betty’, ‘20’, ‘00000000’);
insert into student values(null,‘Jerry’, ‘18’, ‘11111111’);
insert into student values(null,‘Betty’, ‘21’, ‘22222222’);
insert into student values(null,‘Steve’, ‘27’, ‘33333333’);
insert into student values(null,‘James’, ‘22’, ‘44444444’);
commit;
2、建立一個.properties文件用于存儲MySql連接的幾個屬性。為什么要建立.properties而不在代碼里面寫死,由于這個并不是Java設(shè)計模式的分類,就不細(xì)講了,只需要記住:從設(shè)計的角度看,把內(nèi)容寫在配置文件中永遠(yuǎn)好過把內(nèi)容寫死在代碼中。
mysqlpackage=com.mysql.jdbc.Driver
mysqlurl=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8
mysqlname=root
mysqlpassword=root
3、根據(jù)表字段建立實體類
public class Student
{
private int studentId;
private String studentName;
private int studentAge;
private String studentPhone;
public Student(int studentId, String studentName, int studentAge,
String studentPhone)
{
this.studentId = studentId;
this.studentName = studentName;
this.studentAge = studentAge;
this.studentPhone = studentPhone;
}
public int getStudentId()
{
return studentId;
}
public String getStudentName()
{
return studentName;
}
public int getStudentAge()
{
return studentAge;
}
public String getStudentPhone()
{
return studentPhone;
}
public String toString()
{
return “studentId = ” + studentId + “, studentName = ” + studentName + “, studentAge = ” +
studentAge + “, studentPhone = ” + studentPhone;
}
}
4、寫一個DBConnection類專門用于向外提供數(shù)據(jù)庫連接。我這里用了MySql,所以只有一個mysqlConnection,如果還用到了Oracle,當(dāng)然還可以向外提供一個oracleConnection。把這些連接設(shè)為全局的可能有人會想是否會有線程安全問題,這是一個很好的問題。那因為我們只從Connection里面讀取一個PreparedStatement出來,而不會去寫它,只讀不修改,是不會引發(fā)線程安全問題的。另外把Connection設(shè)置為static的保證了Connection在內(nèi)存中只有一份,不會占多大資源,每次使用完不調(diào)用close()方法去關(guān)閉它也沒事。
public class DBConnection
{
private static Properties properties = new Properties();
static
{
/** 要從CLASSPATH下取.properties文件,因此要加“/” */
InputStream is = DBConnection.class.getResourceAsStream(“/db.properties”);
try
{
properties.load(is);
}
catch (IOException e)
{
e.printStackTrace();
}
}
/** 這個mysqlConnection只是為了用來從里面讀一個PreparedStatement,不會往里面寫數(shù)據(jù),因此沒有線程安全問題,可以作為一個全局變量 */
public static Connection mysqlConnection = getConnection();
public static Connection getConnection()
{
Connection con = null;
try
{
Class.forName((String)properties.getProperty(“mysqlpackage”));
con = DriverManager.getConnection((String)properties.getProperty(“mysqlurl”),
(String)properties.getProperty(“mysqlname”),
(String)properties.getProperty(“mysqlpassword”));
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
return con;
}
}
5、建立一個工具類,用來寫各種方法,專門和數(shù)據(jù)庫進(jìn)行交互。這種工具類最好搞成單例的,這樣就不用每次去new出來了(實際上new出來也沒看出來會有什么好處),節(jié)省資源
package com.xrq.test11;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class StudentManager
{
private static StudentManager instance = new StudentManager();
private StudentManager()
{
}
public static StudentManager getInstance()
{
return instance;
}
public List
{
List
Connection connection = DBConnection.mysqlConnection;
PreparedStatement ps = connection.prepareStatement(“select * from student where studentName = ?”);
ps.setString(1, studentName);
ResultSet rs = ps.executeQuery();
Student student = null;
while (rs.next())
{
student = new Student(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4));
studentList.add(student);
}
ps.close();
rs.close();
return studentList;
}
}
6、寫個main函數(shù)去調(diào)用一下
List
studentList = StudentManager.getInstance().querySomeStudents(“Betty”);
for (Student student : studentList)
System.out.println(student);
7、看一下運行結(jié)果,和數(shù)據(jù)庫里面的一樣,成功
studentId = 1, studentName = Betty, studentAge = 20, studentPhone = 00000000
studentId = 3, studentName = Betty, studentAge = 21, studentPhone = 22222222
評論
查看更多