package Examination;
import java.io.*;
import java.sql.*;
public class ConDB {
private String sdbdriver="sun.jdbc.odbc.JdbcOdbcDriver";//指定数据库驱动程序的类名
private String sconnstr="jdbc:odbc:ODBC";//定义连接,将你的ODBC源名,替换ODBC
private Connection conn=null;//内部使用的数据连接对象
private Statement stmt=null;//创建向数据库发送查询和指令的Statement对象
private ResultSet rs=null;//创建结果集对象
//构造函数,建立数据库连接
public ConDB() throws IOException
{
if( conn!=null )
{
conn=null;//如果不为空将其值为空
}
try{
Class.forName( sdbdriver );//在Class.forName()方法中指定数据库驱动程序的类名
}catch( java.lang.ClassNotFoundException e)
{
System.err.println( "Class.forName(): "+e.getMessage() );
}// end of try...catch...
try{
conn=DriverManager.getConnection( sconnstr,"","" );
}catch( SQLException e){
System.out.println( "getConnection(): "+e.getMessage() );
}//end of try...catch...
}//end of method zhaopin()
//...........................................................................
/*
* 此方法用来向数据库发送查询和命令(sql 所接收的字符串即为查询语句)
* 根据 sql 进行查询,得到结果集,若查询不到则抛出异常
*/
public ResultSet executeQuery(String sql) throws SQLException,Exception
{
rs=null;
try{
if(conn!=null)
conn.close();//如果不为空关闭连接
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery( sql );
}catch(SQLException e){
System.err.println( "zhaopin.executeQuery:"+e.getMessage() );
}
return rs;
}//end of method executeQuery()
//.........................................................................
/*
* 此方法向数据库表插入信息(String 接收插入语句)
*/
public void executeInsert( String sql )throws SQLException
{
try{
if(conn!=null)
conn.close();//如果不为空关闭连接
stmt=conn.createStatement();
stmt.executeUpdate( sql );
stmt.close();
conn.close();
}catch(SQLException e){
System.err.println( "zhaopin.executeInsert():"+e.getMessage() );
}
}//end of method executeInsert()
//.........................................................................
/*
* 此方法用来修改数据库表的信息
*/
public void executeUpdate( String sql ) throws SQLException
{
try
{
if(conn!=null)
conn.close();
stmt=conn.createStatement();
stmt.executeUpdate(sql);
conn.commit();
}catch(SQLException e){
System.err.println("zhaopin.executeUpdate:"+e.getMessage());
}
}//end of method executeUpdate()
//.........................................................................
/*
* 删除表中的行数据
*/
public void executeDelete(String sql)throws SQLException
{
try{
if(conn!=null)
conn.close();
stmt=conn.createStatement();
stmt.executeUpdate( sql );
stmt.close();
conn.close();
}catch(SQLException e){
System.err.println( "zhaopin.executeDelete():"+e.getMessage() );
}
}//end of method executeDelete()
//.........................................................................
/*
* 查询符合条件的信息行数
*/
public int getRowNum( String table )throws SQLException
{
int rsnum=0;
try
{
String sql=" select count(*) from "+table+" ";
if(conn!=null)
conn.close();
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery( sql );
while( rs.next() )//得出行数
{
rsnum=rs.getInt(1);
}
}
catch(SQLException q)
{
System.err.println( "zhaopin.getResultNum:"+q.getMessage() );
}
return rsnum;
}//end of method getRowNum)(
//.........................................................................
/*
* 关闭连接
*/
public void closeStmt()//关闭 Statement 对象
{
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public void closeConn()//关闭连接
{
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
下面是一个简单的连接
/*
* 功能:
*
* 实现连接数据库JDBC-ODBC桥
*
*
* 作者:柴志强
* */
package Examination;
import java.sql.*;
public class ConDB{
private Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
public ConDB()throws SQLException{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){
System.err.print("Connection DB():" +e.getMessage());
}
con=DriverManager.getConnection("jdbc:odbc:Examination","","");
}
public ResultSet Query(String sql)throws SQLException{
stmt=con.createStatement();
rs=stmt.executeQuery( sql );
return rs;
}
public int Update( String sql )throws SQLException{
int iTemp=0;
stmt=con.createStatement();
iTemp=stmt.executeUpdate( sql );
return iTemp;
}
}