jdbc连接oracle数据库

时间:2022-09-22 00:01:48
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);}}