当前位置: 技术问答>java相关
连接数据库的问题,有点急~~~~
来源: 互联网 发布时间:2015-05-01
本文导语: 写了好久,总觉得这个JavaBeans不好, 请各位大虾说一说自己怎样连接数据库的?(Oracle) 谢谢! | package database; import java.sql.*; import java.util.*; import sun.io.*; public class OPDB{ DBConnectionMannager connMg...
写了好久,总觉得这个JavaBeans不好,
请各位大虾说一说自己怎样连接数据库的?(Oracle)
谢谢!
请各位大虾说一说自己怎样连接数据库的?(Oracle)
谢谢!
|
package database;
import java.sql.*;
import java.util.*;
import sun.io.*;
public class OPDB{
DBConnectionMannager connMgr;
Connection conn =null;
Statement sqlStatement =null;
ResultSet results =null;
ResultSet retemp =null;
ResultSetMetaData resultsMeta =null;
boolean status;
String sql;
int columns;
long rowcount =0;
//连接数据库
public void connection(){
try{
/*DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
this.conn =DriverManager.getConnection ("jdbc:oracle:thin:@10.10.10.220:1521:market","sz_hk", "sz_hk");
*/
connMgr = DBConnectionMannager.getInstance();
conn =connMgr.getConnection("sz_hk");
this.status =true;
}catch(Exception e)
{
this.status =false;
}
}
//连接数据库
public void connection(String con,String username,String password){
try{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
this.conn =DriverManager.getConnection (con,username,password);
this.status =true;
}catch(SQLException e)
{
this.status =false;
}
}
// 返回连接
public Connection getConn(){
/* try{
DBConnectionMannager connMgr;
connMgr = DBConnectionMannager.getInstance();
Connection conn =connMgr.getConnection("sz_hk");
if(conn !=null)
this.status =true;
else
this.status =false;
}catch(Exception e){
System.out.println( e.toString());
}*/
return this.conn ;
}
// 关闭对数据库的连接等。
public void Close(){
try{
if(this.conn !=null)
this.conn.close();
if(this.sqlStatement !=null)
this.sqlStatement.close();
if(this.results !=null)
this.results.close();
this.connMgr.release();
this.status =true;
}catch(SQLException e){
this.status =false;
}
}
//从数据库中选择数据
public synchronized ResultSet select(Connection c,String sql){
int index =0;
//Assign passed arguments
this.conn =c;
this.sql =sql;
try{
//Create Statement Object and Execute SQL
sqlStatement =this.conn.createStatement();
retemp =sqlStatement.executeQuery(this.sql);
this.rowcount =0;
//Get the column count for the result set
this.rowcount =0;
while(this.retemp.next())
this.rowcount++;
if(retemp!=null)
retemp.close();
results =sqlStatement.executeQuery(this.sql);
resultsMeta =results.getMetaData();
this.columns =resultsMeta.getColumnCount();
this.status =true;
return results;
}catch(SQLException e){
this.status =false;
return null;
}
}
//返回结果集的列数目
public int getColumns(){
return this.columns;
}
//返回结果集的行数目
public long getRowcount(){
return this.rowcount;
}
// 修改数据库中的记录。
public synchronized void update(Connection c,String sql){
try{
Statement sqlStatement =c.createStatement();
sqlStatement.executeUpdate(sql);
status =true;
}catch(SQLException e){
//set status to flase
status =false;
}
}
//向数据库中插入记录。
public synchronized void insert(Connection c,String sql){
try{
Statement sqlStatement =c.createStatement();
sqlStatement.executeUpdate(sql);
//set status variable to true
this.status =true;
}catch(SQLException e){
//set status variable to true
this.status =false;
}
}
// 删除记录 。
public synchronized void delete(Connection c,String sql){
try{
sqlStatement =this.conn.createStatement();
sqlStatement.executeUpdate(sql);
status =true;
}catch(SQLException e){
//set status to flase
status =false;
}
}
//删除所有记录。
public void deleteAll(String table){
String sql ="delete from "+table;
try{
sqlStatement =this.conn.createStatement();
sqlStatement.executeUpdate(sql);
status =true;
}catch(SQLException e){
status =false;
}
}
//返回对数据库的操作是否成功
public boolean getSuccess(){
return this.status;
}
//转换为中文字符
public String GBK(String action){
try{
byte[] b =action.getBytes("GB2312");
String convert =new String(b,"8859_1");
return convert;
}catch(Exception e){
}
return null;
}
public static String AsciiToChineseString(String s)
{
char[] orig =s.toCharArray();
byte[] dest =new byte[orig.length];
for(int i=0;i
import java.sql.*;
import java.util.*;
import sun.io.*;
public class OPDB{
DBConnectionMannager connMgr;
Connection conn =null;
Statement sqlStatement =null;
ResultSet results =null;
ResultSet retemp =null;
ResultSetMetaData resultsMeta =null;
boolean status;
String sql;
int columns;
long rowcount =0;
//连接数据库
public void connection(){
try{
/*DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
this.conn =DriverManager.getConnection ("jdbc:oracle:thin:@10.10.10.220:1521:market","sz_hk", "sz_hk");
*/
connMgr = DBConnectionMannager.getInstance();
conn =connMgr.getConnection("sz_hk");
this.status =true;
}catch(Exception e)
{
this.status =false;
}
}
//连接数据库
public void connection(String con,String username,String password){
try{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
this.conn =DriverManager.getConnection (con,username,password);
this.status =true;
}catch(SQLException e)
{
this.status =false;
}
}
// 返回连接
public Connection getConn(){
/* try{
DBConnectionMannager connMgr;
connMgr = DBConnectionMannager.getInstance();
Connection conn =connMgr.getConnection("sz_hk");
if(conn !=null)
this.status =true;
else
this.status =false;
}catch(Exception e){
System.out.println( e.toString());
}*/
return this.conn ;
}
// 关闭对数据库的连接等。
public void Close(){
try{
if(this.conn !=null)
this.conn.close();
if(this.sqlStatement !=null)
this.sqlStatement.close();
if(this.results !=null)
this.results.close();
this.connMgr.release();
this.status =true;
}catch(SQLException e){
this.status =false;
}
}
//从数据库中选择数据
public synchronized ResultSet select(Connection c,String sql){
int index =0;
//Assign passed arguments
this.conn =c;
this.sql =sql;
try{
//Create Statement Object and Execute SQL
sqlStatement =this.conn.createStatement();
retemp =sqlStatement.executeQuery(this.sql);
this.rowcount =0;
//Get the column count for the result set
this.rowcount =0;
while(this.retemp.next())
this.rowcount++;
if(retemp!=null)
retemp.close();
results =sqlStatement.executeQuery(this.sql);
resultsMeta =results.getMetaData();
this.columns =resultsMeta.getColumnCount();
this.status =true;
return results;
}catch(SQLException e){
this.status =false;
return null;
}
}
//返回结果集的列数目
public int getColumns(){
return this.columns;
}
//返回结果集的行数目
public long getRowcount(){
return this.rowcount;
}
// 修改数据库中的记录。
public synchronized void update(Connection c,String sql){
try{
Statement sqlStatement =c.createStatement();
sqlStatement.executeUpdate(sql);
status =true;
}catch(SQLException e){
//set status to flase
status =false;
}
}
//向数据库中插入记录。
public synchronized void insert(Connection c,String sql){
try{
Statement sqlStatement =c.createStatement();
sqlStatement.executeUpdate(sql);
//set status variable to true
this.status =true;
}catch(SQLException e){
//set status variable to true
this.status =false;
}
}
// 删除记录 。
public synchronized void delete(Connection c,String sql){
try{
sqlStatement =this.conn.createStatement();
sqlStatement.executeUpdate(sql);
status =true;
}catch(SQLException e){
//set status to flase
status =false;
}
}
//删除所有记录。
public void deleteAll(String table){
String sql ="delete from "+table;
try{
sqlStatement =this.conn.createStatement();
sqlStatement.executeUpdate(sql);
status =true;
}catch(SQLException e){
status =false;
}
}
//返回对数据库的操作是否成功
public boolean getSuccess(){
return this.status;
}
//转换为中文字符
public String GBK(String action){
try{
byte[] b =action.getBytes("GB2312");
String convert =new String(b,"8859_1");
return convert;
}catch(Exception e){
}
return null;
}
public static String AsciiToChineseString(String s)
{
char[] orig =s.toCharArray();
byte[] dest =new byte[orig.length];
for(int i=0;i
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!