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);
2. 怎樣使用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);
3. 怎麼用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:@<hostname>:<port>:<sid>
db.user = sa
db.password =
4. java與oracle要怎樣連接
JDBC啊,六大步,
String url = "jdbc:oracle:thin:@localhost:1521:XE";//固定的,照著寫就行
String username = "system";//oracle用戶名
String pwd = "123";//oracle密碼
String sql = "select sum(cost) from orders where userid = ? and orderstatusid = 3";
String sql2 = "select * from vidmember where id = ?";
/*
* 根據id查找出該人消費的總金額。
* */
public int MoneyfindById(int id) {
int a = 0;
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, pwd);
pst = conn.prepareStatement(sql);
pst.setInt(1,id);
rs = pst.executeQuery();
if(rs.next()){
a = rs.getInt("sum(cost)");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a;
}
5. 怎麼實現用java訪問oracle資料庫
用JDBC啊,
首先去oracle網站下個java連接oracle的驅動,www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc9201.html
代碼列子:
import java.sql.*;
public class TestDBOracle {
public static void main(String[] args)
throws ClassNotFoundException, SQLException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
//
// or
// DriverManager.registerDriver
// (new oracle.jdbc.driver.OracleDriver());
String url = "jdbc:oracle:thin:@//server.local:1521/prod";
// jdbc:oracle:thin:@//host:port/service
// or
// String url = "jdbc:oracle:thin:@server.local:1521:prodsid";
// jdbc:oracle:thin:@host:port:SID
Connection conn =
DriverManager.getConnection(url,"scott", "tiger");
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
ResultSet rset =
stmt.executeQuery("select BANNER from SYS.V_$VERSION");
while (rset.next()) {
System.out.println (rset.getString(1));
}
stmt.close();
System.out.println ("Ok.");
}
}
6. 誰教我怎麼用java連 oracle資料庫
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());//Class.forname("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; //127.0.0.1是安裝有Oracle實例的機器IP,當然這里也可以寫機器名,1521是Oracle的埠號,應該沒人在安裝時會改吧,orcl是資料庫實例名
Connection conn = DriverManager.getConnection(url,"scott","tiger");
提示:看自己的機器名,在cmd窗口下輸入hostname,看機器的IP,則輸入ipconfig
這種連接方式用的比較多,是"瘦"連接方式,可以跨機器訪問非本地的Oracle實例,
還有一種連接方式叫做"胖"連接方式,只能連接本地的Oracle實例,前提是本地安裝了的Oracle實例.用的比較少,開發中也不常用.
7. 怎麼用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();
}
}
8. java jdbc連接oracle資料庫步驟
跟連接mysql一樣的;
如果java類連接,網上搜索一個連接代碼即可,若是controller,在裡面寫上
spring.datasource.secondary.jdbc-url=
spring.datasource.secondary.username=
spring.datasource.secondary.password=
oracle同理
9. 求Java 連接Oracle資料庫有哪幾種方式
Sys,它是一個 DBA用戶名,具有最大的資料庫操作許可權,sys 必須以sysdba的身份登錄;
System,它也是一個 DBA用戶名,許可權僅次於 Sys用戶,system 以正常身份登錄,具有一般操作許可權!
DBSNMP 該用戶負責Oracle系統的智能代理(Intelligent Agent),該用戶的預設密碼也是「DBSNMP」。它具有一個比較危險的系統許可權" unlimited tablespace
SYSMAN oracle超級管理員!