java連接oracle資料庫
1. Java連接Oracle資料庫的代碼多少
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:@localhost:1521:orcl";
String user = "sa";
String password = "123456";
Connection conn = DriverManager.getConnection(url,user,password);
2. 用java做界面連接oracle資料庫實現增刪改查功能,具體步驟怎麼做
如果不用ORM框架,那麼直接引入ojdbc6.jar 驅動包,然後在代碼中JDBC配置;
然後可以使用jdbc實現增刪改查,比如下面代碼
ResultSetrs=null;
Statementstmt=null;
Connectionconn=null;
try{
Class.forName("oracle.jdbc.driver.oracleDriver");
//neworacle.jdbc.driver.oracleDriver();
conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:yuewei","scott","tiger");
stmt=conn.createStatement();
rs=stmt.executeQuery("select*fromdept");
while(rs.next()){
System.out.println(rs.getString("deptno"));
//System.out.println(rs.getInt("deptno"));
}
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}catch(SQLExceptione){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(SQLExceptione){
e.printStackTrace();
}
}
2.
3. 怎麼實現用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();
}
4. 怎麼用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();
}
}
5. 如何用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();
}
}
}
6. 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();
}
}
}
7. 如何用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 =
8. 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);
9. java與oracle資料庫相連,用的是哪個jar文件
恩恩,是這個文件,就是class12.jar文件
我安裝的是11g,路徑是E:appinbinproct11.2.0dbhome_1ouijlibclasses12.jar
其它的oracle版本的classes12.jar的位置回,你就網路一下吧答