码迷,mamicode.com
首页 > 数据库 > 详细

jdbc连接oracle数据库

时间:2018-07-11 21:57:53      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:java   throw   red   upd   oracl   加载   ati   get   server   

package cn.xvl.test;

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

public class test {
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		Class.forName("oracle.jdbc.driver.OracleDriver");       //加载数据库驱动
		String url= "jdbc:oracle:thin:@localhost:1521:xe";		//连接URL
		String username = "root";
		String password = "123456";
		Connection connection = DriverManager.getConnection(url, username, password);
		String sql = "insert into stu values (?)";
		PreparedStatement prepareStatement = connection.prepareStatement(sql);
		prepareStatement.setObject(1, 2);
		int executeUpdate = prepareStatement.executeUpdate();
		System.out.println(executeUpdate);
	}
}

select value from v$parameter where name=‘instance_name‘;    //获取oracle的sid  -- 就是上面URL中的xe 

oracle 驱动目录:安装目录D:\oracle\app\oracle\product\11.2.0\server\jdbc\lib下获取

package cn.xvl.test;
import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;
public class test {public static void main(String[] args) throws ClassNotFoundException, SQLException {Class.forName("oracle.jdbc.driver.OracleDriver");       //加载数据库驱动String url= "jdbc:oracle:thin:@localhost:1521:xe";//连接URLString username = "root";String password = "123456";Connection connection = DriverManager.getConnection(url, username, password);String sql = "insert into stu values (?)";PreparedStatement prepareStatement = connection.prepareStatement(sql);prepareStatement.setObject(1, 2);int executeUpdate = prepareStatement.executeUpdate();System.out.println(executeUpdate);}}

 

jdbc连接oracle数据库

标签:java   throw   red   upd   oracl   加载   ati   get   server   

原文地址:https://www.cnblogs.com/deptop/p/9296775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!