当前位置:首页 » 操作系统 » java连接oracle数据库代码

java连接oracle数据库代码

发布时间: 2023-09-27 15:43:00

㈠ 如何用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 =

㈡ 怎么用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数据库

8.Oracle8/8i/9i数据库(thin模式)
//import java.sql.*;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmtNew=conn.createStatement();

1.提取单条记录
//import java.sql.*;
Connection con=null;
Statement stmt=null;
ResultSet %%6=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:"+%%1;
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement();
stmt.executeUpdate(%%4);
%%6=stmt.executeQuery(%%5);
%%7
}catch(Exception e){
e.printStackTrace();
}finally{
try {
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}

2.单值比较
//import java.sql.*;
ResultSet rs;
String strurl="jdbc:odbc:%%1";
Connection con=NULL;
Statement stmt=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(%%2);
rs.next();
int %%3=rs.getInt("%%3");
if(%%3==%%4)
{
%%5
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(rs!=null)
{
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}

3.显示表格
//import java.awt.*;
//import javax.swing.*;
//import java.sql.*;
//import javax.swing.table.*;
String[] colHeads=%%4;
Connection con=null;
Statement stmt=null;
ResultSet %%6=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:"+%%1;
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement();
%%6=stmt.executeQuery("SELECT count(*) as au_count from "+%%5);
%%6.next();
int iCount=rs.getInt("au_count");
Object[][] data=new Object[iCount][];
int i=0;
%%6=stmt.executeQuery("SELECT * from "+%%5);
while(%%6.next()){
data[i]=new Object[iCount];
data[i][0]=%%6.getString("au_fname");
data[i][1]=%%6.getString("Phone");
data[i][2]=%%6.getString("City");
i++;
}
JTable table=new JTable(data,colHeads);
JScrollPane jsp=new JScrollPane(table);
getContentPane().add(jsp);
}catch(Exception e){
e.printStackTrace();
}
finally{
if(con!=null){
try {
%%6.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

7.执行命令
//import java.sql.*;
Connection conn;
PreparedStatement pst=null;
try {
conn=DriverManager.getConnection(url);
pst=conn.prepareStatement("Insert Into grade(%%1) Values (?)");
pst.setInt(1,%%2);
//pst.setInt(2,%%2);
pst.addBatch();
pst.executeBatch();
} catch (SQLException e){
e.printStackTrace();
}
finally{
pst.close();
conn.close();
}

热点内容
上传为防盗链图片 发布:2025-01-23 14:57:11 浏览:301
服务器essd什么意思 发布:2025-01-23 14:51:24 浏览:268
spring上传文件限制 发布:2025-01-23 14:50:30 浏览:310
奇亚币p图软件存储机 发布:2025-01-23 14:38:03 浏览:43
linux有用的命令 发布:2025-01-23 14:35:03 浏览:681
php显示缩略图 发布:2025-01-23 14:22:17 浏览:725
安卓哈利波特怎么更换账号 发布:2025-01-23 14:16:44 浏览:586
中国压缩包 发布:2025-01-23 14:10:49 浏览:499
如果让电脑访问到公司服务器 发布:2025-01-23 14:02:46 浏览:686
360浏览器脚本 发布:2025-01-23 13:54:42 浏览:565