JDBC五大步骤以及相关说明
一·加载JDBC的驱动程序
因为我们连接数据库的时候,我们要加载连接的数据库的驱动到JVM中去。(我们需要调用java.long.class类中的静态方法,forName(String.className)来实现的。)
具体的代码实现
class.forName("com.mysql.jdbc.Driver");
二.创建数据库的连接(URL,username,password)
url是数据文件的目标地址,也就是数据库的地址。
url是由协议,子协议,数据源标识 。(协议:子协议:数据源标识)
协议:在JDBC中总是以jdbc开始
子协议:是桥连接的驱动程序或是数据库管理系统名称。
数据源标识:标记找到数据库来源的地址与连接端口。
url=“jdbc.mysql://localhost:3306/product?useUnicode=true?charactorEncoding=UTF-8”
具体的代码实现
String url=““jdbc.mysql://localhost:3306/product?useUnicode=true?charactorEncoding=UTF-8””
String username="root";
String password="root";
try{
Connection conn= DriverManager.getConnection(url,username,password);
}catch(SQLException se){
System.out.println("数据库连接失败!")
se.printStackTrace();
}
三创建一个Statement(有三种sql语句,静态,动态,执行数据库存储过程)
1执行静态SQL语句。
con.createStatement();
2执行动态sql语句,通常通过PreparedStatement实例实现
PreparedStatement pstms=conn.PreparedStatement();
四:执行SQL语句
Statement接口提供了三种执行SQL语句的方法:executeQuery(查询的方法),executeUpdate(增删改的方法,insert,delete,update相关的方法),和execute
1 查询的方法
ResuletSet rs=conn.executeQuery();
2.增删改的方法
int i =conn.executeUpdate();
五:结果的处理
1.executeQuery返回的是一个ResultSet的数组(结果处理器)
调用getXXX()获得相应的数据
具体的代码实现
while(rs.next()){
String name = rs.getString("name") ;
String pass = rs.getString(1) ; // 此方法比较高效 --如果返回的数据要存入数组和对象中的话,此方法还是比较简便的。
}
六:关闭操作
每一次结果处理完之后,我们都要把相关的jdbc的流关闭,并且释放jdbc的资源。
1.关闭结果集(ResultSet)
2.关闭声明(Statement)
3.关闭连接对象(Connection)
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
实际应用代码
package org.berg.houserent.util;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.berg.houserent.entity.HouseEntity;
import com.mysql.jdbc.Connection;
public class text {
//mysql的驱动程序
private static final String DRIVER = "com.mysql.jdbc.Driver";
//我电脑中mysql的地址
private static final String url = "jdbc:mysql://127.0.0.1:3306/houseforrent?useUnicode=true&characterEncoding=UTF-8";
//账号密码
private static final String user = "root";
private static final String password = "root";
// method get connection
public Connection getConnection() {
Connection conn = null;
try {
Class.forName(DRIVER);
conn = (Connection) DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//method close dateSource
public void closeSource(ResultSet rs,PreparedStatement ps,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//实现查询的方法
public static void main(String[] args) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
text function1=new text();
String sql="select * from t_user";
try {
conn = function1.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
System.out.println(rs.getString("user"));
System.out.println(rs.getString("password"));
}
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
function1.closeSource(rs, ps, conn);
}
}
}