❶ 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 =