oracle连接数据库java
用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.");
}
}
② 如何用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数据库
第一种方法JDBC链接:
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "xxxxx";
String password = "xxxxxx";
connection = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
第二种Spring中Bean配置:
<!-- 配置oracle数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url"
value ="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="xxxxxx" />
<property name="password" value="xxxxxxxxx" />
</bean>
④ 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;
}
⑤ 怎样使用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);