⑴ 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中資料庫的鏈接已經測試過了。