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的位置回,你就网络一下吧答