如何写javabean程序连接oracle

xylfred 2003-03-28 09:26:28
我想写个javabean程序,如何连接oracle 8数据库,实现简单的查找和插入等功能的
有没有哪位哥们帮一下,如试成功,必定给分.最好具体一些.
...全文
11 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
pqds 2003-03-28
http://expert.csdn.net/Expert/FAQ/List_Room_FAQ_Index.asp?bigclassid=54
回复
AllError 2003-03-28
怎么用不用说了吧:
不过连接池的速度好像不行.哪位高手指点一下
回复
AllError 2003-03-28
javabean:

package webbean;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

//声明类库文件
import java.net.*;
import java.sql.*;
import java.lang.*;
import java.io.*;
import java.util.*;
public class Query {
//成员变量初始化
public static boolean isNull;
Connection conn = null; //数据库连接
Statement stmt=null;
ResultSet rs = null; //记录集
//db的构建器
public Query() {
try {
//注册数据库驱动程序为Oracle驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("driver error: " + e.getMessage()); //这样写是为了方便调试程序,出错打印mydb()就知道在什么地方出错了
}
try {
//建立数据库连接,使用Oracle的一种thin连接方式,demo为主机名字,demodb为数据库,后面的两个demo为用户名和密码
conn = DriverManager.getConnection("dbc:oracle:thin:@10.0.2.122:1521:truelx","xingch","xingch_123");
stmt = conn.createStatement();

}
catch(SQLException ex) {
System.err.println("stmt error: " + ex.getMessage());
}
}

//executeQuery方法用于进行记录的查询操作

//入口参数为sql语句,返回ResultSet对象
public ResultSet executeQuery(String sql) {
try{
rs = null;
//执行数据库查询操作
conn.setAutoCommit(true);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("Query error: " + ex.getMessage());
}
return rs;
}


//发送非附件消息
//入口参数为sql语句,成功返回true,否则为false
public void executeUpdate(String sql) {
rs = null;
try {
//建立数据库连接,其它参数说明同上面的一样
conn.setAutoCommit(true);
int rowCount = stmt.executeUpdate(sql);
//如果不成功,bupdate就会返回0
}
catch(SQLException ex) {
//打印出错信息
System.err.println("db.executeUpdate: " + ex.getMessage());
}
}
//toChinese方法用于将一个字符串进行中文处理
//否则将会是???这样的字符串
public void close(){
try{
if (rs!=null)
{
rs.close();
}
if(stmt!=null)
stmt.close();
if (conn!=null)
{
conn.close();
}
}catch(SQLException e){
System.err.println("close error:"+e.getMessage());
}
}

public static String toChinese(String strvalue) {
try{
if(strvalue==null)
{
return null;
}
else
{
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
}catch(Exception e){
return null;
}
}
public static void main(String[] args)

{
java.util.Date t1= new java.util.Date();
Query mm=new Query();

try{
ResultSet rs=mm.executeQuery("select * from mo_everday where rownum<2000 ");

String example;
while(rs.next()){
example=rs.getString(2);
System.out.println(example);

}
}catch(SQLException e){
System.err.println("close error:"+e.getMessage());

}

java.util.Date t2= new java.util.Date();
long dif = t2.getTime() - t1.getTime();
System.out.println(dif);
System.out.println("Hello World!");

}

}
回复
AllError 2003-03-28
连接池:
package webbean;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

import java.sql.*;
import java.io.*;
import javax.sql.*;
import javax.naming.*;
import oracle.jdbc.pool.*;

public class OraPooledSQL
{

private PooledConnection dbpool;

/**
@param ConnectionURL 连接名 如: jdbc:odbc:myODBC
@param UserID 用户名
@param PassWord 用户密码
*/
public OraPooledSQL(String ConnectionURL,String UserID,String PassWord)
{

try{

OracleConnectionPoolDataSource ocpds = new OracleConnectionPoolDataSource();

ocpds.setURL(ConnectionURL);

ocpds.setUser(UserID);

ocpds.setPassword(PassWord);

dbpool = ocpds.getPooledConnection();

}
catch(Exception ex)
{
System.err.println("Error in PooledSQL-construct : ");
ex.printStackTrace(System.err);
}

}//end OraPooledSQL

//close dbpool
protected void finalize()
{
if( dbpool != null )
{
try
{
dbpool.close();
}
catch(Exception ex)
{
}
}
}

/**
用于更新、添加或删除的SQL语句
@param SQL SQL语句字串,如:insert into tablename values(id,......)
*/
public int Update(String SQL)
{
Statement stmt = null;
int rc = 0;

Connection connection = null;

try
{
connection = dbpool.getConnection();
stmt = connection.createStatement();
rc = stmt.executeUpdate(SQL);
}

catch( Exception ex )
{
System.err.println("Error in Update - OraPooledSQL : ");
ex.printStackTrace(System.err);
}

return rc;

} //end Update()


/**
用于查询的SQL语句
@param SQL SQL语句字串,如:select * from tablename
*/
public ResultSet Query(String SQL)
{
Statement stmt = null;
ResultSet rs = null;
Connection connection = null;

try
{
connection = dbpool.getConnection();
stmt = connection.createStatement();
rs = stmt.executeQuery(SQL);
}

catch( Exception ex )
{
System.err.println("Error in Query - SQLBean : ");
ex.printStackTrace(System.err);
}

return rs;

} //end Query
public static void main(String[] args) {
java.util.Date t1= new java.util.Date();
try{
OraPooledSQL test1 = new OraPooledSQL("dbc:oracle:thin:@10.0.2.122:1521:truelx","xingch","xingch_123");
ResultSet rs = test1.Query("select * from mo_everday where rownum<1000 ");
while(rs.next()){
System.out.println(rs.getString(2));
}
}catch(SQLException ex){
System.out.println("error");
}
java.util.Date t2= new java.util.Date();
long dif = t2.getTime() - t1.getTime();
System.out.println(dif);
System.out.println("list over");
}

} //end Class
回复
freeever 2003-03-28
简单应用:

Connection connection = null;
Statement stmt = null;
String strDriver = "oracle.jdbc.driver.OracleDriver";
//xxx是数据库的全局名(SID)
String strURL = "jdbc:oracle:thin:@192.168.210.88:1521:xxx";
String strUser = "username";
String strPwd = "password";
String strSQL = "select * from tablename";

try {
Class.forName(strDriver);
connection = DriverManager.getConnection(strURL, strUser, strPwd);
stmt = connection.createStatement();
//查询
ResultSet rs = stmt.executeQuery(strSQL);
String str1=null
String str2 = null;
int int1 = 0;
int int2 = 0;;

while (rs.next()) {
str1 = rs.getString("fieldname1");
str2 = rs.getString("fieldname2");
int1 = rs.getInt("fieldname3");
int2 = rs.getInt("fieldname4");
}

//修改
strSQL="update tablename set fieldname1='value1' where ... ...";
stmt.executeUpdate(strSQL);

}
catch (SQLException ex) {
ex.printStackTrace();
}
catch (ClassNotFoundException ec) {
ec.printStackTrace();
}
finally {
try {
if (stmt != null) {
stmt.close();
}
}
catch (SQLException ex) {
System.out.println("statement.close() exception");
}
try {
if (connection != null) {
connection.close();
}
}
catch (SQLException ex) {
System.out.println("connection.close() exception");
}
}
回复
IT老兵 2003-03-28
风老弟都说完了,俺就没什么好说的了,哈哈。
回复
垲垲 2003-03-28
这是我刚刚试过的成功了,你要把主机和数据库密码修改以下就可以了!

把main去掉,编译一下就成了bean了,用于获取数据库连接!

public class conoracle
{
private java.sql.Connection conOracle = null;
public boolean iscon = false;

public static void main(String args[])
{
conoracle cm = new conoracle();
cm.connectiondb("192.169.1.227","oracle8","system","manager");
System.out.println("ok");
try
{
java.sql.Statement stmt = null;
java.sql.ResultSet rs = null;
stmt = cm.conOracle.createStatement();
for(int i=20;i<23;i++)
stmt.execute("insert into tomtest1 values('a测试"+i+"','bm"+i+"','cm"+i+"')");

rs = stmt.executeQuery("select * from tomtest1");
while(rs.next())
{
System.out.println(rs.getString("A")+"--"+rs.getString("b")+"--"+rs.getString("C"));
}
rs.close();
stmt.close();
cm.conOracle.close();
}catch(Exception e)
{System.out.println(e);}
}

public java.sql.Connection connectiondb(String host,String db,String uname,String pwd)
{
try
{
Class.forName("com.inet.ora.OraDriver").newInstance();
iscon = true;
}
catch (Exception e)
{
System.out.println("connection err!\n" + e);
iscon = false;
}
try
{
String connDBParam = "jdbc:inetora:" + host + ":1521:" + db;
conOracle = DriverManager.getConnection(connDBParam,uname,pwd);
iscon = true;
}
catch (SQLException se)
{
System.out.println("connection err!" + se);
iscon = false;
}
return conOracle;
}

}
回复
yzxasd 2003-03-28
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为你的数据库的SID String user="scott";
String password="tiger";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
rs.close();
stmt.close();
conn.close();
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2003-03-28 09:26
社区公告
暂无公告