jdbc连接oracle数据库
Posted 花l信风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdbc连接oracle数据库相关的知识,希望对你有一定的参考价值。
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:oracleapporacleproduct11.2.0serverjdbclib下获取
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数据库的主要内容,如果未能解决你的问题,请参考以下文章