java操作oracle
用eclipse
首先在oracle文件下找到jdbc文件,里面的lib文件下有个class12.zip
在eclipse里新建个项目,建好后在左边右击那个项目图标,选择属性(最下面的),里面有个java build path,点进去,找到add external jars按键,点一下,把那个class12.zip加载进来,然后可以开始写代码了。
我的代码如下:
import java.sql.*;
public class connect {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
String url="jdbc:oracle:thin:@服务器ip地址:端口号:数据库名字";
String user="用户名";
String password="密码";
String str="INSERT INTO ZZZ_2 VALUES('041110018','JHDK')";
con = java.sql.DriverManager.getConnection(url,user,password);
// 创建状态
stmt = con.createStatement();
// 执行SQL语句,返回结果集
//int rowcount = stmt.executeUpdate(str);
int j = stmt.executeUpdate("update ZZZ_2 set NAME='dbt' where ID=21");
int k = stmt.executeUpdate("delete from ZZZ_2 where ID=41110020");
rs = stmt.executeQuery("SELECT * FROM ZZZ_2");
// 对结果集进行处理
while (rs.next()) {
int id = rs.getInt("ID");
String name = rs.getString("NAME");
//Integer age = rs.getObject("age") == null ? null : rs.getInt("age");
System.out.println(id + ": " + name);
} }catch(SQLException e){
e.printStackTrace();}
// 释放资源
finally{
try{
rs.close();
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
stmt.close();
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
con.close();
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
注意,上面的ip地址什么的要填对,ZZZ_2是我随便建的表,你改成自己的表名就行了
‘贰’ 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
一个最简单连接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数据库,需要详细解释
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数据库
这个是完全没有问题的。
先期准备:
1、两台电脑必须要联网,能够访问到
2、将oracle的驱动程序拷贝到你的java的电脑上,并设置到环境变量中。
java代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DbTest {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement prstm = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@ip:port:orcl"; //数据库连接字符串,需要替换ip,port,orcl
String user="user"; //用户名,需要替换你的数据库的用户名
String password="pswd"; //密码,需要替换为对应的密码
conn = DriverManager.getConnection(url,user,password); //获取连接
prstm = conn.prepareStatement("SELECT 1 TEST_ID FROM DUAL");
rs = prstm.executeQuery();
while (rs.next()) {
System.out.println("查询结果为:" + rs.getInt("TEST_ID"));
}
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//使用完毕进行相关内容的关闭,注意相关顺序
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (prstm != null) {
try {
prstm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}