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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值