java数据库编程

连接Oracle数据库

在java.sql包中定义有一个Connection接口,这个接口表示的是数据连接,也就是说没一个连接都在程序中使用一个Connection接口对象实现。但是如果要想取得Connection接口对象,则必须通过Java.sqlDriverManager类取得,在此类中定义有如下一个方法:
public Connection getConnection(String url,String user,String password)throws SQLException,如果要进行连接,则getConnection方法需要连接地址,用户名,密码。
实际上如果要进行数据库的连接则需要四个核心内容:

  • 驱动程序:由不同的生产厂商提供,对于oracle数据库,会自动将驱动程序保存到安装目录中:F:\INSTALL\oracle\product\11.2.0\dbhome_1\jdbc\lib.ojdbc6.jar中。
    oracle驱动名称:oracle.jdbc.driver.OracleDriver
    可以通过Class.forName()进行数据库驱动程序的加载
  • 数据库连接地址:不同的数据库有自己不同的数据库连接地址
    格式:jdbc:oracle:thin:@IP地址:默认端口:SID
  • 用户名:如scott
  • 密码:如tiger

使用Statement操作数据库

  如果想要进行数据库的操作可以使用java.sql.Statement接口(public interface Statement extends Wrapper, AutoCloseable)完成处理。但是如果想要取得Statement接口对象,必须依靠Connection接口完成,在Connection接口里定义有如下方法:

创建Statement对象:public Statement createSatement() throws SQLException
有了Statement对象之后就可以使用如下方法进行数据库的操作:
数据的更新操作:public int executeUpdate(String sql) throws SQLException当执行完更新操作之后返回影响的行数
数据的查询操作:public ResultSet executeQuerry(String sql) throws SQLException


使用PreparedStatement操作数据库

PreparedStatement类定义如下:public interface PreparedStatement
extends Statement

要想取得PreparedStatement接口 实例化对象,则需要使用java.sql.Connection接口处理,在Connection定义有方法:

  • PreparedStatement prepareStatement(String sql) throws SQLException
    这里面的sql是需要通过占位符?来进行描述,编号从1开始

而取得PreparedStatement类对象之后就可以使用里面的方法:

  • 更新操作:public int executeUpdate() throws SQLException
  • 查询操作:public ResultSet executeQuery() throws SQLException.
  • 填充操作:public void setXxx(int index,数据类型 变量);

在使用PreparedStatement接口操作的Date数据的时候特别要引起注意:

使用的是java.sql.Date类,而在实际的程序之中描述日期的一定是java.util.Date类型,而在java.util.Date类中有三个子类:Date(日期)、Time(时间)、Timestamp(日期时间);这三个子类都有一个类似的构造方法:

  • java.sql.Date类:public Date(long date);
  • java.sql.Time类:public Time(long time);
  • java.sql.Timestamp类:public Timestamp(long time)。
    而在java.util.Date类中有一个getTime()方法可以将Date变为long。

范例:使用PreparedStatement查询全部数据

package cn.pyl.jdbcdemo;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBCPreparedStatementDemo {
    public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
    public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:mldn";
    public static final String DBUSER = "scott";
    public static final String DBPASSWORD = "tiger";

    public static void main(String[] args) throws Exception {
        // 进行数据库的驱动加载
        Class.forName(DBDRIVER);
        // 数据库连接
        Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
        String sql = " SELECT mid,name,age,birthday,note FROM member ";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        ResultSet rst = pstmt.executeQuery();
        while (rst.next()) {
            int mid = rst.getInt(1);
            String name = rst.getString(2);
            int age = rst.getInt(3);
            Date birthday = rst.getDate(4);
            String note = rst.getString(5);
            System.out.println(mid + "、" + name + "、" + age + "、" + birthday + "、" + note);
        }
        conn.close();
    }
}

范例:根据mid进行查询

package cn.pyl.jdbcdemo;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBCPreparedStatementDemo {
    public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
    public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:mldn";
    public static final String DBUSER = "scott";
    public static final String DBPASSWORD = "tiger";

    public static void main(String[] args) throws Exception {
        // 进行数据库的驱动加载
        Class.forName(DBDRIVER);
        // 数据库连接
        Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
        String sql = " SELECT mid,name,age,birthday,note FROM member WHERE mid=? ";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, 5);
        ResultSet rst = pstmt.executeQuery();
        while (rst.next()) {
            int mid = rst.getInt(1);
            String name = rst.getString(2);
            int age = rst.getInt(3);
            Date birthday = rst.getDate(4);
            String note = rst.getString(5);
            System.out.println(mid + "、" + name + "、" + age + "、" + birthday + "、" + note);
        }
        conn.close();
    }
}

范例:模糊查询

package cn.pyl.jdbcdemo;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;  
public class JDBCPreparedStatementDemo {
    public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
    public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:mldn";
    public static final String DBUSER = "scott";
    public static final String DBPASSWORD = "tiger";
    public static void main(String[] args) throws Exception {
        String column = "name";// 表示在哪个列上面操作
        String keyWord = "李";// 模糊查询的关键字
        // 进行数据库的驱动加载
        Class.forName(DBDRIVER);
        // 数据库连接
        Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
        String sql = " SELECT mid,name,age,birthday,note FROM member WHERE " + column + " LIKE ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, "%" + keyWord + "%");
        ResultSet rst = pstmt.executeQuery();
        while (rst.next()) {
            int mid = rst.getInt(1);
            String name = rst.getString(2);
            int age = rst.getInt(3);
            Date birthday = rst.getDate(4);
            String note = rst.getString(5);
            System.out.println(mid + "、" + name + "、" + age + "、" + birthday + "、" + note);
        }
        conn.close();
    }
}

范例:分页查询

package cn.pyl.jdbcdemo;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.omg.CORBA.PRIVATE_MEMBER;

public class JDBCPreparedStatementDemo {
    public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
    public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:mldn";
    public static final String DBUSER = "scott";
    public static final String DBPASSWORD = "tiger";

    public static void main(String[] args) throws Exception {
        String column = "name";// 表示在哪个列上面操作
        String keyWord = "李";// 模糊查询的关键字
        int currentPage = 1;
        int lineSize = 3;
        // 进行数据库的驱动加载
        Class.forName(DBDRIVER);
        // 数据库连接
        Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
        // String sql = " SELECT mid,name,age,birthday,note FROM member WHERE "
        // + column + " LIKE ? ";
        String sql = "SELECT * FROM (" + " SELECT mid,name,age,birthday,note,ROWNUM rn " + " FROM member WHERE "
                + column + " LIKE ? AND ROWNUM <= ? ) temp " + " WHERE temp.rn > ? ";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, "%" + keyWord + "%");
        pstmt.setInt(2, currentPage * lineSize);
        pstmt.setInt(3, (currentPage - 1) * lineSize);
        ResultSet rst = pstmt.executeQuery();
        while (rst.next()) {
            int mid = rst.getInt(1);
            String name = rst.getString(2);
            int age = rst.getInt(3);
            Date birthday = rst.getDate(4);
            String note = rst.getString(5);
            System.out.println(mid + "、" + name + "、" + age + "、" + birthday + "、" + note);
        }
        conn.close();
    }
}

范例:统计查询

package cn.pyl.jdbcdemo;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.omg.CORBA.PRIVATE_MEMBER;

public class JDBCPreparedStatementDemo {
    public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
    public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:mldn";
    public static final String DBUSER = "scott";
    public static final String DBPASSWORD = "tiger";

    public static void main(String[] args) throws Exception {
        String column = "name";// 表示在哪个列上面操作
        String keyWord = "李";// 模糊查询的关键字
        int currentPage = 1;
        int lineSize = 3;
        // 进行数据库的驱动加载
        Class.forName(DBDRIVER);
        // 数据库连接
        Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
        // String sql = " SELECT mid,name,age,birthday,note FROM member WHERE "
        // + column + " LIKE ? ";
        String sql = " SELECT COUNT(*) FROM member WHERE " + column + " LIKE ? ";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, "%" + keyWord + "%");
        // pstmt.setInt(2, currentPage * lineSize);
        // pstmt.setInt(3, (currentPage - 1) * lineSize);
        ResultSet rst = pstmt.executeQuery();
        while (rst.next()) {
            // int mid = rst.getInt(1);
            // String name = rst.getString(2);
            // int age = rst.getInt(3);
            // Date birthday = rst.getDate(4);
            // String note = rst.getString(5);
            // System.out.println(mid + "、" + name + "、" + age + "、" + birthday
            // + "、" + note);
            int count = rst.getInt(1);
            System.out.println("数据总量 : " + count);
        }
        conn.close();
    }
}

其实使用PreparedStatement操作数据库模式基本上是固定的,只需要更改sql语句就可以


批处理与事务处理

批处理

所谓的批处理指的是在一次数据更新之中可以同时发送多条更新语句一起执行。
Statement接口的批处理支持:

  • 追加批处理语句:public void addBatch(String sql) throws SQLException
  • 批量执行sql:public int[] executeBatch() throws SQLException

PreparedStatement对批处理的支持:

  • 追加批处理操作:public void addBatch() throws SQLException

事务处理

所谓的事务指的就是所有的操作要么一起成功,要么一起失败。也就是传统的关系型数据库都支持的ACID原则。但是在JDBC里面对于事物也有着原始的支持,所有的事务控制都在Connection接口中定义:

  • 设置是否自动提交:public void setAutoCommit(boolean autoCommit) throws SQLException
  • 提交事务:public void commit() throws SQLException
  • 回滚事务:public void rollback() throws SQLException

你可能感兴趣的:(数据库,jdbc-oracle)