JDBC初步
2009-10-20 23:01 BlueDream 阅读(907) 评论(0) 编辑 收藏 举报Sun公司为java与数据库之间提供了一套接口API.通过这套API可以通过数据库厂商提供的驱动类Driver来操作数据库.而这套API就是所谓的JDBC.
java应用程序.JDBC.数据库三者的关系如下:
下面介绍一下连接数据库的基本步骤:
1.注册驱动
Class.forName("com.mysql.jdbc.Driver"); // 有好几种方式推荐这种
2.建立连接
DriverManager.getConnection(url, username, password); //参数分别为数据服务器地址.用户名.密码
3.创建执行SQL的语句Statement
conn.createStatement();
4.执行语句返回结果集
rs = st.executeQuery("select * from users");
5.处理结果集ResultSet
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t\t"
+ rs.getString(3) + "\t\t" + rs.getString(4));
}
6.释放资源(很重要.连接对于数据库是稀有资源.不按时释放会导致最后内存减少而宕机)
System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t\t"
+ rs.getString(3) + "\t\t" + rs.getString(4));
}
try {
if (rs != null) {
rs.close(); // 关闭结果集
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close(); // 关闭Statement
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close(); // 关闭连接
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
if (rs != null) {
rs.close(); // 关闭结果集
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close(); // 关闭Statement
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close(); // 关闭连接
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
下面给演示下基本的JDBC模板.下面有两个类 一个是模板类一个是测试类
模板类DBUtils.java
package com.test.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author Administrator
* 模板类DBUtils
*/
public final class DBUtils {
// 参数定义
private static String url = "jdbc:mysql://localhost:3306/mytest"; // 数据库地址
private static String username = "root"; // 数据库用户名
private static String password = "root"; // 数据库密码
private DBUtils() {
}
// 加载驱动
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("驱动加载出错!");
}
}
// 获得连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
// 释放连接
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close(); // 关闭结果集
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close(); // 关闭Statement
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close(); // 关闭连接
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author Administrator
* 模板类DBUtils
*/
public final class DBUtils {
// 参数定义
private static String url = "jdbc:mysql://localhost:3306/mytest"; // 数据库地址
private static String username = "root"; // 数据库用户名
private static String password = "root"; // 数据库密码
private DBUtils() {
}
// 加载驱动
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("驱动加载出错!");
}
}
// 获得连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
// 释放连接
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close(); // 关闭结果集
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close(); // 关闭Statement
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close(); // 关闭连接
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
模板测试类Base.java
package com.test.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Base {
public static void main(String[] args) {
temple();
}
public static void temple() {
Statement st = null;
ResultSet rs = null;
Connection conn = null;
try {
conn = DBUtils.getConnection();
st = conn.createStatement();
rs = st.executeQuery("select * from users");
System.out.println("age\tlastname\tfirstname\tid");
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t\t"
+ rs.getString(3) + "\t\t" + rs.getString(4));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtils.free(rs, st, conn);
}
}
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Base {
public static void main(String[] args) {
temple();
}
public static void temple() {
Statement st = null;
ResultSet rs = null;
Connection conn = null;
try {
conn = DBUtils.getConnection();
st = conn.createStatement();
rs = st.executeQuery("select * from users");
System.out.println("age\tlastname\tfirstname\tid");
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t\t"
+ rs.getString(3) + "\t\t" + rs.getString(4));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtils.free(rs, st, conn);
}
}
}
显示正常查询结果为:
age lastname firstname id
22 啡 咖 1
3434 434 ni 2
22 啡 咖 1
3434 434 ni 2
这只是JDBC的第一步以后会更深入的学习和分析.