我的连库的JAVABEAN是这样的,我试过了,可以关库,以及对其各种操作。
package dbconn;import java.sql.*;
import java.io.*;
import java.util.*;
import java.net.URL;
import java.io.InputStream;
import java.util.Properties;public class DBconn{
String dns="";
String host="";
String port="";
String type="";
String dbDriver= "";
Connection conn = null;
Statement stmt = null;
String dataSource = "";
String username = "";
String password = "";
ResultSet rs=null;
String eHint = "";
String dept;
String isconn;
//open connection
public boolean OpenConnection(){
try
{
dbDriver= "com.sybase.jdbc2.jdbc.SybDriver";
Class.forName(dbDriver);
//与DBMS建立连接
conn=DriverManager.getConnection("jdbc:sybase:Tds:16.1.1.10:6666", "xf","xufeng");//"jdbc:sybase:Tds:192.168.0.215:6666", "nms", "enetmanager"
return true;
}catch (SQLException e1){
eHint= "connection:" + e1.getMessage();
return false;
}catch (ClassNotFoundException e2){
eHint="Unable to load the Sybase JDBC driver:"+ e2.getMessage();
return false;
}catch(Exception e3){
eHint = "Open db configure file:"+e3.getMessage();
return false;
}
}
//执行select
public ResultSet getResultSet(String sql){
rs=null;
try{
// 创建一个 Statement 对象,使我们可以向驱动程序提交 SQL 语句
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
eHint= "getResultSet:"+ex.getMessage();
}
return rs;
}
//执行update
public boolean executeQuery(String sql)
{
try{
// 创建一个 Statement 对象,使我们可以向驱动程序提交 SQL 语句
stmt = conn.createStatement();
// 提交查询,创建 ResultSet 对象
stmt.executeUpdate(sql);
return true;
}catch(SQLException ex) {
eHint= "executeQuery: " + ex.getMessage();
return false;
}
}
//关闭连接
public boolean closeConn()
{
try{
if (rs!=null) rs.close();
if (stmt!=null) stmt.close();
if (conn!=null) conn.close();
return true;
}catch(SQLException ex) {
eHint= "closeConn: " + ex.getMessage();
return false;
}
}
}
package dbconn;import java.sql.*;
import java.io.*;
import java.util.*;
import java.net.URL;
import java.io.InputStream;
import java.util.Properties;public class DBconn{
String dns="";
String host="";
String port="";
String type="";
String dbDriver= "";
Connection conn = null;
Statement stmt = null;
String dataSource = "";
String username = "";
String password = "";
ResultSet rs=null;
String eHint = "";
String dept;
String isconn;
//open connection
public boolean OpenConnection(){
try
{
dbDriver= "com.sybase.jdbc2.jdbc.SybDriver";
Class.forName(dbDriver);
//与DBMS建立连接
conn=DriverManager.getConnection("jdbc:sybase:Tds:16.1.1.10:6666", "xf","xufeng");//"jdbc:sybase:Tds:192.168.0.215:6666", "nms", "enetmanager"
return true;
}catch (SQLException e1){
eHint= "connection:" + e1.getMessage();
return false;
}catch (ClassNotFoundException e2){
eHint="Unable to load the Sybase JDBC driver:"+ e2.getMessage();
return false;
}catch(Exception e3){
eHint = "Open db configure file:"+e3.getMessage();
return false;
}
}
//执行select
public ResultSet getResultSet(String sql){
rs=null;
try{
// 创建一个 Statement 对象,使我们可以向驱动程序提交 SQL 语句
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
eHint= "getResultSet:"+ex.getMessage();
}
return rs;
}
//执行update
public boolean executeQuery(String sql)
{
try{
// 创建一个 Statement 对象,使我们可以向驱动程序提交 SQL 语句
stmt = conn.createStatement();
// 提交查询,创建 ResultSet 对象
stmt.executeUpdate(sql);
return true;
}catch(SQLException ex) {
eHint= "executeQuery: " + ex.getMessage();
return false;
}
}
//关闭连接
public boolean closeConn()
{
try{
if (rs!=null) rs.close();
if (stmt!=null) stmt.close();
if (conn!=null) conn.close();
return true;
}catch(SQLException ex) {
eHint= "closeConn: " + ex.getMessage();
return false;
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货