本文主要是介绍JDBC(韩顺平Oracle),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java连接Oracle有两种方式,一种是JDBC,另一种是JDBC-ODBC桥接模式,后者基本上很少用,甚至在JDK1.8中都没有后者的驱动了,这里主要是前者。举例说明:创建一个Java Project。
创建帮助类SQLHelper.java:
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;public class SQLHelper {// 定义三个变量private static Connection ct = null;private static PreparedStatement ps = null;private static ResultSet rs = null;// 连接数据库的用户名,密码,url驱动// 实际开发中,把这些变量写到外部文件中// 当程序启动时候,我们读入这些配置信息 java.util.Properitesprivate static String username = null;private static String password = null;private static String driver = null;private static String url = null;// 类实例化之前就执行,即编译就执行了// 使用静态块加载驱动static{try {// 使用Properties类,来读取配置文件Properties pp = new Properties();FileInputStream fis = null;try {fis = new FileInputStream("dbinfo.properties");pp.load(fis);// 取出username,password,driver,urlusername = (String) pp.get("username");password = (String) pp.get("password");driver = (String) pp.get("driver");url = (String) pp.get("url");}catch (IOException e) {e.printStackTrace();}finally{try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}// 统一的cud操作public static void executeUpdate(String sql,String[] parameters){try {ct = DriverManager.getConnection(url);ps = ct.prepareStatement(sql);if (parameters != null) {for (int i = 0; i < parameters.length; i++) {ps.setString(i + 1, parameters[i]);}ps.executeQuery();}} catch (Exception e) {e.printStackTrace();throw new RuntimeException(e.getMessage());}finally{close(rs, ps, ct);}}//完成查询任务// sql 表示要执行的sql语句public static ResultSet executeQuey(String sql,String[] parameters){//对SQL语句赋值try {// 得到连接ct = DriverManager.getConnection(url, username, password);// 创建ps对象 SQL语句对象ps = ct.prepareStatement(sql);// 如果paramters不为null,我们就去赋值 if (parameters != null) {for (int i = 0; i < parameters.length; i++) {ps.setString(i+1, parameters[i]);}}rs = ps.executeQuery();} catch (SQLException e) {e.printStackTrace();throw new RuntimeException(e.getMessage());}finally{//close(rs,ps,ct);此处不能关闭}return rs;}// 关闭资源public static void close(ResultSet rs,Statement ps,Connection ct){if (rs != null) {try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (ps != null) {try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (rs != null) {try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static Connection getCt() {return ct;}public static PreparedStatement getPs() {return ps;}}
测试类Test:
import java.sql.*;public class Test{// 此程序演示对oracle的Query操作public static void main(String[] args) {sel();}public static void sel() {String sql = "select * from emp";ResultSet rs = SQLHelper.executeQuey(sql, null);try {while(rs.next()){System.out.println(rs.getString("ename"));}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{SQLHelper.close(rs, SQLHelper.getPs(), SQLHelper.getCt());}}
}
数据库信息dbinfo.properties:
username = scott
password = tiger
driver = oracle.jdbc.driver.OracleDriver
url =jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
这篇关于JDBC(韩顺平Oracle)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!