JDBC数据库编程(一) - 三大对象及基本步骤

今天我们来讲一下在Java中如何利用JDBC访问数据库的基本步骤。

熟悉微软的ADO的朋友一定知道,在MS中访问数据库有三大对象,它们是:
Connection(连接), Command(命令), RecordSet(记录集)

我们要说,在JDBC中我们也有对应的三大对象:
[b]
Connection (连接)
Statement (语句)
ResultSet (结果集)
[/b]

[color=red]
注意: ResultSet不要与RecordSet搞混。
[/color]

既然有了三大对象,那下面我们就来看一下JDBC访问数据库的基本步骤。

[b]
[color=blue]
1) 获取Connection(连接)。
2) 利用连接创建Statement(语句)。
3) 利用Statement(语句)传入实际sql语句操作(查询,更新)数据库。
4) 关闭连接。
[/color]
[/b]

其中,连接本身包括五大要素:
[b]
[color=red]
a. driver (数据库驱动名)
b. source url (数据库URL)
c. user name (连接数据库的用户名)
d. user password (连接数据库的密码)
e. Connection (连接本身)
[/color]

获取连接的基本步骤如下:
[color=blue]
1) 装载驱动。 (eg: Class.forName("驱动名");)
2) 根据url,用户名,密码取得连接。(DriverManager)
[/color]
[/b]

我们可以把连接封装在一个类中,具体代码如下:

package corejava2.jdbc;

import java.sql.*;

public class DbConn {
private String driver = "oracle.jdbc.driver.OracleDriver";
private String srcUrl = "jdbc:oracle:thin:@localhost:1521:OracleDB";
private String user = "train";
private String password = "train";
private Connection conn = null;

public DbConn() {
try {
Class.forName(driver);
this.conn = DriverManager.getConnection(srcUrl, user, password);
} catch (Exception e) {
System.out.println("Get DB Connection Error!");
}
}

public Connection getConnection() {
return this.conn;
}

public void close() {
try {
this.conn.close();
} catch (Exception e) {
System.out.println("Close DB Connection Error!");
}
}
}


下面我们就来做个基本的JDBC程序来显示我们例子数据库(Oracle)中的Customers
表的数据。

首先我们需要做个Customer类,用于获取数据库中的单行记录:

package corejava2.objects;

public class Customer {
private String customerId;
private String city;

public Customer(String customerId, String city) {
this.customerId = customerId;
this.city = city;
}

public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}

public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}

public String toString() {
return "Id: " + customerId + " , City: " + city;
}
}


接下来,是客户端的访问代码:

package corejava2.jdbc;

import java.util.*;
import java.sql.*;
import corejava2.objects.Customer;

public class SimpleQuery {
public static void main(String[]args)
throws Exception{
DbConn conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList customers = null;
String sql = "select * from Customers";

conn = new DbConn();

try {
stmt = conn.getConnection().createStatement();
rs = stmt.executeQuery(sql);

customers = loadCustomers(rs);

rs.close();
stmt.close();
conn.close();

showCustomers(customers);
} catch (SQLException e) {
e.printStackTrace();
}

}

private static ArrayList loadCustomers(ResultSet rs)
throws Exception {

ArrayList customerList = new ArrayList();

while (rs.next()) {
String customerId = rs.getString("CUSTOMERID");
String city = rs.getString("CITY");

Customer customer = new Customer(customerId,city);
customerList.add(customer);
}

return customerList;
}

private static void showCustomers(ArrayList list) {
Iterator it = list.iterator();
while (it.hasNext()) {
Customer customer = (Customer)it.next();
System.out.println(customer);
}
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JDBC(Java Database Connectivity)是Java语言访问关系型数据库的标准API,下面是JDBC编程基本步骤: 1. 加载数据库驱动:使用Class.forName()方法加载数据库驱动程序。 2. 建立数据库连接:使用DriverManager.getConnection()方法建立与数据库的连接。 3. 创建SQL语句:使用SQL语句或PreparedStatement预处理语句操作数据库。 4. 执行SQL语句:使用Statement或PreparedStatement接口的execute()或executeUpdate()方法执行SQL语句。 5. 处理查询结果:使用ResultSet接口获取查询结果并进行相应的处理。 6. 关闭连接:使用Connection接口的close()方法关闭数据库连接。 以下是一个简单示例: ```java import java.sql.*; public class JDBCDemo { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { //1. 加载数据库驱动 Class.forName("com.mysql.jdbc.Driver"); //2. 建立数据库连接 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; conn = DriverManager.getConnection(url, user, password); //3. 创建SQL语句 String sql = "SELECT * FROM user"; //4. 执行SQL语句 stmt = conn.createStatement(); rs = stmt.executeQuery(sql); //5. 处理查询结果 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); System.out.println("id: " + id + ", name: " + name); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { //6. 关闭连接 try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 以上是JDBC编程基本流程,当然在实际应用中还需要注意事务处理、连接池等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值