JDBC
JAVA Database Connectivity
java数据库
sun公司提供的一种数据库访问规则,规范,由于数据库种类较多,并且java语言使用比较广泛,sun就通过了一种规范,让其他的数据库提供商去实现的访问规则
JDBC的基本步骤
1.注册驱动
2.建立连接
3.创建statement
4.执行sql,得到ResultSet
5.遍历结果集
6.释放资源
package tan.jung.test;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import tan.jung.JDBCUtil;
public class MianTest {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs =null;
try {
//注册驱动
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//建立连接
conn = DriverManager.getConnection("jdbc:mysql://localhost/student", "root", "root");
//创建statement
st= conn.createStatement();
//得到结果集
String sql="select * from tab_belong";
rs = st.executeQuery(sql);
//遍历查询每一条记录
while(rs.next()){
int id = rs.getInt("id");
String belong = rs.getString("belong");
System.out.println("id="+id+",belong="+belong);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
JDBCUtil.release(conn, st, rs);
}
}
}
//第二个包
package tan.jung;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtil {
public static void release(Connection conn,Statement st,ResultSet rs){
closers(rs);
closest(st);
closeconn(conn);
}
private static void closers(ResultSet rs){
try {
if(rs !=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
rs=null;
}
}
private static void closest(Statement st){
try {
if(st !=null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
st=null;
}
}
private static void closeconn(Connection conn){
try {
if(conn !=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
conn=null;
}
}
}
JDBC 工具类构建
1.资源释放工作的整合
2.驱动防二次注册
Class.forName("com.mysql.jdbc.Driver");
3.使用properties配置文件
package tan.jung.test; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import tan.jung.JDBCUtil; public class MianTest { public static void main(String[] args) { Connection conn = null; Statement st = null; ResultSet rs =null; try { conn =JDBCUtil.getConn(); //创建statement st= conn.createStatement(); //得到结果集 String sql="select * from tab_belong"; rs = st.executeQuery(sql); //遍历查询每一条记录 while(rs.next()){ int id = rs.getInt("id"); String belong = rs.getString("belong"); System.out.println("id="+id+",belong="+belong); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtil.release(conn, st, rs); } } } //第二个包 package tan.jung; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class JDBCUtil { static String driverClass = null; static String url=null; static String name=null; static String password=null; static{ try { //1.创建一个属性配置对象 Properties properties = new Properties(); //InputStream is =new FileInputStream("jdbc.properties"); InputStream is =JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"); //导入输出流 properties.load(is); //读取属性 driverClass=properties.getProperty("driverClass"); url=properties.getProperty("url"); name=properties.getProperty("name"); password=properties.getProperty("password"); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConn(){ Connection conn =null; try { Class.forName(driverClass); //建立连接 conn = DriverManager.getConnection(url, name, password); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void release(Connection conn,Statement st,ResultSet rs){ closers(rs); closest(st); closeconn(conn); } private static void closers(ResultSet rs){ try { if(rs !=null){ rs.close(); } } catch (SQLException e) { e.printStackTrace(); }finally { rs=null; } } private static void closest(Statement st){ try { if(st !=null){ st.close(); } } catch (SQLException e) { e.printStackTrace(); }finally { st=null; } } private static void closeconn(Connection conn){ try { if(conn !=null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); }finally { conn=null; } } }
创建jdbc.properties文件
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1/student
name=root
password=root