❶ java要和oracle 11g連接該如何配置oracle
java連接Oracle11G:
1,找到驅動包,在oracle安裝目錄下我的是D:\Oracle\proct\11.1.0\db_1\jdbc\lib;此目錄下有ojdbc5.jar,ojdbc6.jar,如果jdk5就用5,6就用6;
2,配置驅動包的變數(我把驅動包拷出來,放在d:\java\util裡面),配置後上面MYSQL的類似。
3,測試代碼:
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.SQLException ;
import java.sql.PreparedStatement ;
public class JDBCOracle{
// 定義資料庫驅動程序
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
// 定義資料庫的連接地址
public static final String DBURL = "jdbc:oracle:thin:@localhost:埠號:資料庫標識符" ;
//埠號後標識符可以通過在doc下運行lsnrctl status查看 default:1521
// 資料庫的連接用戶名
public static final String DBUSER = "scott" ;
// 資料庫的連接密碼
public static final String DBPASS = "tiger" ;
public static void main(String args[]) throws Exception{ // 所有異常拋出
Connection conn = null ; // 資料庫連接
Class.forName(DBDRIVER) ; // 載入驅動程序
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
PreparedStatement pstmt = null ;
String sql = "INSERT INTO person(id,name,age,birthday) VALUES (myseq.nextVal,?,?,?)" ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,"tomcat") ;
pstmt.setInt(2,30) ;
pstmt.setDate(3,new java.sql.Date(new java.util.Date().getTime())) ;
pstmt.executeUpdate() ; // 執行更新操作
pstmt.close() ;
conn.close() ; // 資料庫關閉
}
};
❷ 怎麼實現用java訪問oracle資料庫
實現用java訪問oracle資料庫:
准備工作:
1.下載驅動,打開瀏覽器,搜索oracle資料庫驅動,進入下載頁面下載即可;
2.將驅動導入到項目中。
用java訪問oracle資料庫代碼實現:
try{
Class.forName("oracle.jdbc.driver.OracleDriver");//載入驅動
//與資料庫建立連接
try{
Stringurl="jdbc:oracle:thin:@localhost:1521:orcl";
Stringuser="scott";//用戶名
Stringpassword="tiger";//密碼
conn=DriverManager.getConnection(url,username,password);
}catch(SQLExceptione){
e.printStackTrace();
}
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
❸ 怎麼用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 資料庫的程序工具類
一個最簡單連接oracle的例子
package oracletest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OracleTest {
public static void main(String[] args) {
//String serverName = "localhost";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("1");
String url = "jdbc:oracle:thin:@localhost:1521:ORA9I";
Connection conn = DriverManager.getConnection(url, "scott", "tiger");
System.out.println("2");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test1");
while (rs.next()) {
System.out.println(rs.getString("id"));
System.out.println(rs.getString("name"));
}
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
}
❺ 如何用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 =