一、 建立与数据的连接
(1)装载合适的JDBC驱动
try
{
//注册数据库驱动程序为Oracle驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (java.lang.ClassNotFoundException e)
{
//这样写是为了方便调试程序,出错打印mydb()就知道在什么地方出错了
System.err.println("mydb(): " + e.getMessage());
}
利用Class.forName 来显式的给应用程序装载JDBC驱动
(2)建立与数据库的连接
try
{
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orc8i", "java", "88888888");
}
catch (SQLException ex)
{
System.err.println("conn:"+ex.getMessage());
}
java.sql.DriverManager是java中管理JDBC驱动一个基本类。
As part of its initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property
(DriverManager类在初始化时将加载jdbc.drivers属性中定义的JDBC驱动)
A program can also explicitly load JDBC drivers at any time. For example, the my.sql.Driver is loaded with the following statement: 举例:
Class.forName("oracle.jdbc.driver.OracleDriver");
当调用getConnection方法时,DriverManager会装载合适的驱动
如果你装载的驱动程序识别了提供给 DriverManager.getConnection 的 JDBC URL ,那个驱动程序将根据 JDBC URL 建立一个到指定 DBMS 的连接。正如名称所示,DriverManager 类在幕后为你管理建立连接的所有细节。除非你是正在写驱动程序,你可能无需使用此类的其它任何方法,一般程序员需要在此类中直接使用的唯一方法是 DriverManager.getConnection。
DriverManager.getConnection 方法返回一个打开的连接,你可以使用此连接创建 JDBC statements 并发送 SQL 语句到数据库。在前面的例子里,conn 对象是一个打开的连接。
二、 利用已经创建的数据库连接进行数据操作(通过Statement类实现)
(1)创建Statement对象
Statement stmt = conn.createStatement();
(2)java.sql.Statement接口的说明
The object used for executing a static SQL statement and returning the results it produces
来对象用来执行一个静态的SQL脚本,并且返回处理的结果。
By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved (交叉存取)with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.
缺省,每个Statement对象在同一时间仅仅可以打开一个Resultset对象。
Statement interface的所有方法在执行时,如果有一个活动的Rsesulset 对象,都隐式的关闭一个ResultSet 对象。
两个方法介绍:
executeQuery(String sql)
Executes the given SQL statement, which returns a single ResultSet object.
executeUpdate(String sql)
Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
(3)java.sql.ResultSet接口说明
A table of data representing a database result set, which is usually generated by executing a statement that queries the database.
A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.
A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce ResultSet objects that are scrollable and/or updatable. The following code fragment, in which con is a valid Connection object, illustrates how to make a result set that is scrollable and insensitive to updates by others, and that is updatable. See ResultSet fields for other options.
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");
// rs will be scrollable, will not show changes made by others,
// and will be updatable
可以使用while循环语句来取出ResultSet对象的对应值:
while (rs.next())
{
System.out.println("username: "+ rs.getString(1));
System.out.println("password: "+rs.getString(2));
System.out.println("email: " +rs.getString(3));
}
(4)应用举例
package counter;
import java.net.*;
import java.sql.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.driver.*;
public class db
{
Connection conn = null; //数据库连接
ResultSet rs = null; //记录集
String Username=""; //用户名
String Password=""; //密码
String Email=""; //email
String Homepage=""; //主页
String Signs=""; //签名
//db的构建器
public db()
{
try
{
//注册数据库驱动程序为Oracle驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (java.lang.ClassNotFoundException e)
{
//这样写是为了方便调试程序,出错打印mydb()就知道在什么地方出错了
System.err.println("mydb(): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs = null;
try
{
//建立数据库连接,使用Oracle的一种thin连接方式,demo为主机名字,demodb为数据库,后面的两个demo为用户名和密码
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orc8i", "java", "88888888");
Statement stmt = conn.createStatement();
//执行数据库查询操作
rs = stmt.executeQuery(sql);
}
catch (SQLException ex)
{
System.err.println("db.executeQuery: " + ex.getMessage());
}
return rs;
}
public boolean executeUpdate(String sql)
{
boolean bupdate = false;
rs = null;
try
{
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orc8i", "java", "88888888");
Statement stmt = conn.createStatement();
int rowCount = stmt.executeUpdate(sql);
//如果不成功,bupdate就会返回0
if (rowCount != 0)
bupdate = true;
}
catch (SQLException ex)
{
//打印出错信息
System.err.println("db.executeUpdate: " + ex.getMessage());
}
return bupdate;
}