java如何连接Oracle(80分)

  • 主题发起人 主题发起人 只为美媚2
  • 开始时间 开始时间

只为美媚2

Unregistered / Unconfirmed
GUEST, unregistred user!
我的QQ:175412977
1.要用那几个包文件(最好发给我)
2.要import那几个东东
3.java连接Oracle的语句
谢谢大家
 
1、你安装oracle的时候有一个文件class12.jar,那个就是oracle自己带的jdbc。
2、import那个jar就可以
3、
package DBstep;
import java.sql.*;
import java.lang.*;
import java.text.*;
import java.util.*;
public class iDBManager2000 {
public String ClassString=null;
public String ConnectionString=null;
public String UserName=null;
public String PassWord=null;

public Connection Conn;
public Statement Stmt;
public PreparedStatement prestmt=null;
public int concurType=ResultSet.CONCUR_READ_ONLY;
public int scrollType=ResultSet.TYPE_SCROLL_INSENSITIVE;

//构造函数(初始化)
public iDBManager2000() {


ClassString="oracle.jdbc.driver.OracleDriver";
ConnectionString="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为你的数据库的SID
UserName=...;
PassWord=...;
}
//For BBS Conn
public java.sql.Connection getBBSConn(){
try{
Class.forName(ClassString).newInstance();
if ((UserName==null) &&
(PassWord==null))
{
Conn= DriverManager.getConnection(ConnectionString);
}else
{
Conn= DriverManager.getConnection(ConnectionString,UserName,PassWord);
}
}catch(Exception e){
e.printStackTrace();
System.out.println("LICHAO");
}
return Conn ;
}
//打开数据库
public boolean OpenConnection()
{
boolean mResult=true;
try
{
Class.forName(ClassString).newInstance();
if ((UserName==null) &&
(PassWord==null))
{
Conn= DriverManager.getConnection(ConnectionString);
}
else
{
Conn= DriverManager.getConnection(ConnectionString,UserName,PassWord);
}
Stmt=Conn.createStatement(scrollType,concurType);
mResult=true;
}
catch(Exception e)
{
System.out.println(e.toString());
mResult=false;
}
return (mResult);
}

public String getStitle()
{
return stitle;
}
public String getFtitle()
{
return ftitle;
}
//关闭数据库连接
public void CloseConnection()
{
try
{
Stmt.close();
Conn.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
//取得当前时间
public String GetDateTime()
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String mDateTime=formatter.format(cal.getTime());
return (mDateTime);
}
//执行查询语句
public ResultSet ExecuteQuery(String SqlString)
{
ResultSet result=null;
try
{
result=Stmt.executeQuery(SqlString);
}
catch(Exception e)
{
System.out.println(e.toString());
}
return (result);
}
//执行修改语句
public int ExecuteUpdate(String SqlString)
{
int result=0;
try
{
result=Stmt.executeUpdate(SqlString);
}
catch(Exception e)
{
System.out.println(e.toString());
}
return (result);
}
//获得当前页面的第一条记录
public int getPageResult(int pageSize,int pageNumber)
{
int result=0;
try
{
result=pageSize*(pageNumber-1);
}
catch(Exception e)
{
System.out.println(e.toString());
}
if(result==0)
{ result=1;
}
return (result);
}

//获得总页数
public int getPageCount(int pageSize,int pageCount)
{
int result=0;
try
{
if(pageCount%pageSize==0)
{
result=pageCount/pageSize;
}
else
{
result=(int)(pageCount/pageSize+1);
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
return (result);
}
//获得总记录数
public int getRecordCount(String SqlString)
{
ResultSet result=null;
int recordCount=0;
try
{
result=Stmt.executeQuery(SqlString);
if(result==null)
{
return (0);
}
else

{
result.next();
recordCount=result.getInt("count");
}

}
catch(Exception e)
{
System.out.println(e.toString());
}
return (recordCount);
}
//封装的修改方法
public int executeUpd (String SqlString)
{
int result=0;
try
{
prestmt=Conn.prepareStatement(SqlString);
Conn.setAutoCommit(true);
result=Stmt.executeUpdate(SqlString);
Conn.commit();
}
catch(Exception e)
{
System.out.println(e.toString());
}
return (result);
}

}
 
首先保证载入Oracle的驱动,以前有讨论,可以参照:
classes12.jar is JDK1.2 or later's oracle driver package.
if you install Oracle client edition, it in jdbc/lib under installation directory.
two path to execute you program:
1 add classes12.jar to you enviorment variant;
win2k: set CLASSPATH=%CLASSPATH%;/classes12.jar
linux: export CLASSPATH=$CLASSPATH:xxx/classes12.jar
2 java option
java -classpath xxx/classes12.jar;
AppClass
note:end of ';'
以后的连接同其他,
 
Oracle安装后,在其软件中存在一个classes12.zip文件,将其更名为classes12.jar(*.jar)文件,然后在你的工程中引用此外部包或在环境变量中设置classpath就可以进行引用了。
对于基本语句,可以参考yangxiao_jiang贴的代码
 
使用包便可,和一般数据库一样、
 

Similar threads

D
回复
0
查看
871
DelphiTeacher的专栏
D
D
回复
0
查看
930
DelphiTeacher的专栏
D
D
回复
0
查看
945
DelphiTeacher的专栏
D
D
回复
0
查看
777
DelphiTeacher的专栏
D
后退
顶部