当前位置:首页 » 数据仓库 » java连接oracle数据库代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

java连接oracle数据库代码

发布时间: 2023-01-18 04:33:51

⑴ java如何连接oracle数据库,要详细代码!!

package com.sp;

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

//演示 如何使用 jdbc_odbc桥连接方式
public class TestOracle {

public static void main(String[] args) {
try {

// 1.加载驱动
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// 2.得到连接
Connection ct = DriverManager.getConnection(
"jdbc.odbc:testConnectOracle", "scott",

"tiger");

// 从下面开始,和SQL Server一模一样
Statement sm = ct.createStatement();
ResultSet rs = sm.executeQuery("select * from emp");
while (rs.next()) {
//用户名
System.out.println("用户名: "+rs.getString(2));
//默认是从1开始编号的
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

⑵ java连接Oracle数据库

Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver");//加入oracle的驱动,“”里面是驱动的路径

String url = "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL";// 数据库连接,oracle代表链接的是oracle数据库;thin:@MyDbComputerNameOrIP代表的是数据库所在的IP地址(可以保留thin:);1521代表链接数据库的端口号;ORCL代表的是数据库名称

String UserName = "root";// 数据库用户登陆名 ( 也有说是 schema 名字的 )

String Password = "2006";// 密码

conn = DriverManager.getConnection(url, UserName, Password);

⑶ java与oracle数据库相连,用的是哪个jar文件

class12.jar文件。

⑷ 如何用java连接oracle数据库

public class OracleJdbcTest
{
String driverClass = "oracle.jdbc.driver.OracleDriver";

Connection con;

public void init(FileInputStream fs) throws Exception
{
Properties props = new Properties();
props.load(fs);
String url = props.getProperty("db.url");
String userName = props.getProperty("db.user");
String password = props.getProperty("db.password");
Class.forName(driverClass);

con=DriverManager.getConnection(url, userName, password);
}

public void fetch() throws SQLException, IOException
{
PreparedStatement ps = con.prepareStatement("select SYSDATE from al");
ResultSet rs = ps.executeQuery();

while (rs.next())
{
// do the thing you do
}
rs.close();
ps.close();
}

public static void main(String[] args)
{
OracleJdbcTest test = new OracleJdbcTest();
test.init();
test.fetch();
}
}

[代码] db.properties
db.url = jdbc:oracle:thin:@::
db.user = sa
db.password =

⑸ 怎么用java连接oracle数据库,需要详细解释

1)首先你要有java连接oracle数据库的驱动类,下载好后导入到工程里

2)代码:
import java.sql.*;

public class JDBCTest {

/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args)
throws ClassNotFoundException, SQLException{
//1.准备参数
String ip = "192.168.0.26";
String sid = "tarena";
String port = "1521";
String dbUser = "openlab";
String dbPassword = "open123";
String driver
= "oracle.jdbc.driver.OracleDriver";

//2.构造驱动实例
Class.forName(driver);

//3.创建连接
//连接字符串是固定的形式,oracle的形式:
String url
= "jdbc:oracle:thin:@"
+ ip + ":" + port + ":" + sid;
Connection conn
= DriverManager.getConnection
(url, dbUser, dbPassword);
//4.执行SQL语句
String sql = "select id, password, name from user_sd1104 " +
"where id = 1001 and password = '1234'";
Statement stmt = conn.createStatement();
ResultSet rs
= stmt.executeQuery(sql);//执行sql语句
while(rs.next()){
int id = rs.getInt(1);
String pwd = rs.getString(2);
String name = rs.getString(3);
System.out.println("welcome," + name);
}
rs.close();
stmt.close();
conn.close();
}

}

⑹ Java连接Oracle数据库的代码多少

上面那里已经写的很不错了,user就是数据库用户名字,password就是密码,url就是你的数据库的路径,
url路径就是jdbc:oracle:@+主机的ip地址(本机数据库就可以用localhost):加端口号:加数据库名字

⑺ JAVA中连接数据库的代码 请教

private static String url="jdbc:oracle:thin:@localhost:1521:xe";
声明一个字符串用于存储数据库连接信息,jdbc:oracle:thin:@localhost:1521表示你要连接的是oracle数据库地址是本机
xe为本机数据库库名。
private static String driverName="oracle.jdbc.driver.OracleDriver";
这一条是声明一个字符串存储数据库驱动

⑻ Java连接Oracle数据库的代码多少

public class DBConnection {

/**
* 获取数据连接
* */
public static Connection getConnection(String ip,String port,String sid,String userName,String passWord ){
String driverName="oracle.jdbc.driver.OracleDriver";
String dbUrl="jdbc:oracle:thin:@"+ip+":"+port+":"+sid+"";
Connection conn=null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(dbUrl, userName, passWord);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;

}

}

⑼ 如何用Java实现连接Oracle

1、工作环境:myeclipse中->新建java_oracle工程->新建包com.zp->新建java_oracle_jdbc.java类
2、需要配置Oracle数据库驱动
java工程(java_oracle)—>右键属性(Properties)->Java Build Path->Libraries->Add External JARs->添加oracle安装目录E:\oracle\proct\10.1.0\Db_1\jdbc\lib下的“classes12.jar”文件;
每次新建一个不同工程都需要配置一次
3、代码如下:
package com.zp;
import java.sql.*;
public class java_oracle_jdbc {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//得到连接
Connection ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:simlink", "scott", "tiger");

Statement sm=ct.createStatement();
ResultSet rs=sm.executeQuery("select * from emp");
while(rs.next())
{
System.out.println("用户名:"+rs.getString(2));
}
rs.close();
sm.close();
ct.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

⑽ java 实现 jdbc连接oracle 数据库的java代码怎么写

package com.jdbc.test;

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

public class JDBCTest {
public static void main(String[] args) throws Exception {
//1.加载驱动
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");

//2.创建数据库连接对象
//Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=db","sa","sqlpass");
//Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8","root","123456");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","Oracle123");

//3.创建数据库命令执行对象
Statement stmt = conn.createStatement();
// PreparedStatement ps = conn.prepareStatement("select * from t_user");

//4.执行数据库命令
ResultSet rs = stmt.executeQuery("select * from t_user");
// ResultSet rs = ps.executeQuery();

//5.处理执行结果
while (rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
System.out.println(id + "\t" + username + "\t" + password);
}

//6.释放数据库资源
if (rs != null) {
rs.close();
}
// if (ps != null) {
// ps.close();
// }
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}

这是以前写的3中数据库的链接已经测试过了。