java连接oracle

拷贝oracle客户端的jar包

D:\jp\oracle\ojdbc14-10.2.0.4.0.jar


D:\jp\oracle\application.datasource.properties

dao.jdbc.url=jdbc:oracle:thin:@192.168.250.131:1521:testdb
dao.jdbc.username=scott
dao.jdbc.password=oracle123


D:\jp\oracle\OracleTest.java

import java.io.FileInputStream;
import java.util.Properties;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class OracleTest{
	
	
	
	public static void main(String[] args) throws Exception {
			System.out.println("OracleTest");
			
			FileInputStream is = new FileInputStream("application.datasource.properties");;
			Properties properties = new Properties();
			
			properties.load(is);
			
			String jdbcUrl = properties.getProperty("dao.jdbc.url");
			String jdbcUser = properties.getProperty("dao.jdbc.username");
			String jdbcPwd = properties.getProperty("dao.jdbc.password");
			
			System.out.println(" url " + jdbcUrl + " username " +jdbcUser + " password " +jdbcPwd );
			
			//要求JVM查找并加载指定的类,也就是说JVM会执行该类的静态代码段
			//oracle.jdbc.driver.OracleDriver 来源于包 ojdbc14-10.2.0.4.0.jar
			Class.forName("oracle.jdbc.driver.OracleDriver");
			
			Connection cn = DriverManager.getConnection(jdbcUrl, jdbcUser , jdbcPwd);
			cn.setAutoCommit(false);
			Statement st = cn.createStatement();
			
			String  charVar;
			double  doubleVar;
			int  intVar;
			
			ResultSet res = st.executeQuery("SELECT to_char(sysdate,'yyyymmddhh24miss'), 0.5, 500 FROM dual");
			while(res.next()){
					charVar = res.getString(1);
					doubleVar = res.getDouble(2);
					intVar = res.getInt(3);
					
					System.out.println(" charVar " + charVar + " doubleVar " +doubleVar + " intVar " +intVar );
					
			}
			
			
			
			res.close();
			st.close();
			
			
			


      //cn.prepareStatement("update your_table set col_name ='xxx' and col_name = "+id).execute();
      //cn.commit();

			cn.close();
	}
	
	
}





编译

D:\jp\oracle>javac  OracleTest.java


执行

D:\jp\oracle>java -classpath ".;.\*" OracleTest



使用绑定变量


import java.io.FileInputStream;
import java.util.Properties;

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


public class OracleTest2{
	
	
	
	public static void main(String[] args) throws Exception {
			System.out.println("OracleTest");
			
			FileInputStream is = new FileInputStream("application.datasource.properties");;
			Properties properties = new Properties();
			
			properties.load(is);
			
			String jdbcUrl = properties.getProperty("dao.jdbc.url");
			String jdbcUser = properties.getProperty("dao.jdbc.username");
			String jdbcPwd = properties.getProperty("dao.jdbc.password");
			
			System.out.println(" url " + jdbcUrl + " username " +jdbcUser + " password " +jdbcPwd );
			
			//要求JVM查找并加载指定的类,也就是说JVM会执行该类的静态代码段
			//oracle.jdbc.driver.OracleDriver 来源于包 ojdbc14-10.2.0.4.0.jar
			Class.forName("oracle.jdbc.driver.OracleDriver");
			
			Connection cn = DriverManager.getConnection(jdbcUrl, jdbcUser , jdbcPwd);
			cn.setAutoCommit(false);
			
			PreparedStatement pst;
			
			String  charVar;
			int  intVar;
			
			pst = cn.prepareStatement("SELECT 'aa' FROM dual where 1=?");
			intVar = 1;
			pst.setInt (1,intVar);
			
			ResultSet res = pst.executeQuery();
			while(res.next()){
					charVar = res.getString(1);
					
					System.out.println(" charVar " + charVar );
					
			}
			
			
			
			res.close();
			pst.close();
			
			
			


      //cn.prepareStatement("update your_table set col_name ='xxx' and col_name = "+id).execute();
      //cn.commit();

			cn.close();
	}
	
	
}



D:\jp\oracle>javac  OracleTest2.java

D:\jp\oracle>java -classpath ".;.\*" OracleTest2



posted on 2016-05-24 17:18  阮減显  阅读(244)  评论(0编辑  收藏  举报

导航