关于JDCB连接oracle数据库。

1:什么是JDBC(百度可详细解释)

              JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用 Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序,同时,JDBC也是个商标名。
             他是一套用于执行SQL语句的javaAPI,应用程序可通过这套API连接到关系数据库,并使用SQL语句来完成对数据库数据的CRUD(数据库的查询,更新,修改以及删除)的操作,这节详细讲解关于jdbc连接Oracle数据库。
     
           2:关于jdbc连接数据库的常用api,这些api主要位于java.sql包下,该包定义了一系列的接口和类。
 
 DriverManager:用于管理JDBC驱动的服务类。程序中使用该类的的主要功能是获取Connection对象,该类包含如下方法:
public static Connection getConnection(String url, String user, String password) throws SQLException
该方法获得url对应数据库的连接;
 
Connection:代表数据库连接对象,每个Connection代表一个物理连接会话。要想访问数据库,必须先得到数据库连接。该接口的常用方法如下:
              Statement createStatement() throws SQLException; 该方法返回一个Statement对象;
               PreparedStatement prepareStatement(String sql)throws SQLException;该方法返回预编译的Statement对象,即将SQL语句提交到数据库进行预编译;
              CallableStatement prepareCall(String sql) throws SQLException;
              该方法返回CallableStatement对象,该对象用于调用存储过程。
              上面上个方法都返回用于执行sql语句的Statement对象,PreparedStatement和CallableStatement是Statement的子类,只有获得了Statement之后才可以执行sql语句;
除此之外,Connection还有如下几个用于控制事务的方法。
              Savepoint setSavepoint() throws SQLException;创建一个保存点;
      Savepoint setSavepoint(String name) throws SQLException;以指定名字来创建一个保存点;
    void setTransactionIsolation(int level) throws SQLException;设置事务的隔离级别;
    void rollback() throws SQLException;回滚事务;
    void rollback(Savepoint savepoint) throws SQLException;将事务回滚到指定的保存点;
    void setAutoCommit(boolean autoCommit) throws SQLException;关闭自动提交,打开事务;
    void commit() throws SQLException;提交事务;
 
Statement:用于执行sql语句的工具接口。该对象既可以执行DDL,DCL语句,也可以用于执行DML语句,还可以用于执行sql查询。当执行sql查询时,返回查询到的结果集。它的常用方法如下:
ResultSet executeQuery(String sql) throws SQLException;该方法用于执行查询语句,并返回查询结果对应ResultSet对象。该方法只能用于执行查询语句。
int executeUpdate(String sql) throws SQLException;该方法用于执行DML语句,并返回受影响的行数;该方法也可用于执行DDL语句,执行DDL语句将返回0;
boolean execute(String sql) throws SQLException;改方法可以执行任何sql语句。如果执行后第一个结果为ResultSet对象,则返回true;如果执行后第一个结果为受影响的行数或没有任何结果,则返回false;
 
PreparedStatement:预编译的Statement对象,PreparedStatement是Statement的子接口,它允许数据库预编译sql语句(这些sql语句通常带有参数),以后每次只改变sql命令的参数,避免数据库每次都需要编译sql语句,无需再传入sql语句,
只要为预编译的sql语句传入参数值即可。所以它比Statement多了如下方法:
void setXxx(int parameterIndex, Xxx value):该方法根据传入参数值的类型不同,需要使用不同的方法。传入的值根据索引传给sql语句中指定位置的参数。
 
ResultSet:结果集对象。该对象包含访问查询结果的方法,ResultSet可以通过列索引或列名获得列数据。它包含了如下常用方法来移动记录指针。
void close() throws SQLException;释放ResultSet对象;
boolean absolute( int row ) throws SQLException;将结果集的记录指针移动到第row行,如果row是负数,则移动到倒数第row行,如果移动后的记录指针指向一条有效记录,则该方法返回true;
boolean next() throws SQLException;将结果集的记录指针定位到下一行,如果移动后的记录指针指向一条有效的记录,则该方法返回true;
boolean last() throws SQLException;将结果集的记录指针定位到最后一行,如果移动后的记录指针指向一条有效的记录,则该方法返回true;
 
       3:简单介绍了JDBC常用的几个接口和类,下面是具体的代码实现。
      3-1:首先我们写连接的获取。我们把它写成一个类。
        public class oracleJdbc {
// 获得conn连接
private static Connection getConn() {
String driver = "oracle.jdbc.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String username = "scott";
String password = "tiger";
Connection conn = null;
try {
Class.forName(driver);
try {
conn = (Connection) DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
写了这个连接类,我们接下来的操作就简单多了,我们接下来的对数据库的操作就简单了很多,不管是增删改查,我们就可以直接调用这个连接类就行了。
// 插入数据
private static int insert(Student student) {
Connection conn = getConn();
int i = 0;
String sql = "insert into students (Name,Sex,Age) values(?,?,?)";
PreparedStatement pstmt = null;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getSex());
pstmt.setString(3, student.getAge());
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
     //修改数据
private static int update(Student student) {
Connection conn = getConn();
int i = 0;
String sql = "update students set Age='" + student.getAge() + "' where Name='" + student.getName() + "'";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
i = pstmt.executeUpdate();
System.out.println("resutl: " + i);
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
     // 查询全部数据
private static Integer getAll() {
Connection conn = getConn();
String sql = "select * from scott.students";
// String sql = "select * from scott.emp";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement)conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
int col = rs.getMetaData().getColumnCount();
System.out.println("============================");
while (rs.next()) {
for (int i = 1; i <= col; i++) {
System.out.print(rs.getString(i) + "\t");
if ((i == 2) && (rs.getString(i).length() < 8)) {
System.out.print("\t");
}
}
System.out.println("");
}
System.out.println("============================");
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
     //删除数据
private static int delete(String name) {
Connection conn = getConn();
int i = 0;
String sql = "delete from students where Name='" + name + "'";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
i = pstmt.executeUpdate();
System.out.println("resutl: " + i);
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
       //测试类
public static void main(String args[]) {
oracleJdbc.getAll();
//oracleJdbc.insert(new Student("Achilles", "Male", "14"));
// oracleJdbc.getAll();
// oracleJdbc.update(new Student("Bean", "", "7"));
// oracleJdbc.delete("Achilles");
// oracleJdbc.getAll();
}
}
这个学生类是提前写好的。测试类可以直接操作上面的四个方法对数据库进行CRUD操作。
这样jdbc对数据库的操作就完成了。

猜你喜欢

转载自www.cnblogs.com/smile908/p/10571219.html
今日推荐