java与oracle连接
Ⅰ java与oracle连接
192.168.3.9是数据库ip地址,1521数据库的访问端口
Ⅱ 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数据库
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();
}
Ⅳ 怎样使用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);
Ⅳ oracle与Java连接方法
import java.sql.Connection; import java.sql.DriverManager; import java.这样连接基本上没有什么错误。你可以试试,很可能就是你的oracle服务没有启动
Ⅵ java连接oracle
应该是这里错了
ResultSet ji1 = (ResultSet) cs.getObject(6);
改成
ResultSet ji1 = (ResultSet) cs.getObject(5);
设值是1开始,取值从0开始。
Ⅶ java操控oracle建立连接
你这样发出来大家是无法给出判断是哪里错误的。最起码得把错误信息提示给贴出来吧。conn是一个connection的连接对象。不管是ORacle还是其他的数据库总是要拿这个连接对象。因为,它们就是jdbc的东西。通过驱动管理对象DriverManager获得一个连接。所以,这里url,user,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();
}
}