java数据库链接程序
import java.sql.*;
import java.io.*;
import java.util.*;
public class DatabaseLink {
String sDB;//从属性文件获取的数据库驱动名
String sUser,sPassword;//从属性文件获取的用户名和密码
String sDataBaseDriver; //jdbc-odbc桥:sun.jdbc.odbc.JdbcOdbcDriver
String sJdbcOdbcConnHead="jdbc:odbc:";//jdbc-odbc驱动的头
Connection conn=null;
Statement stmt=null;
/*
建立数据库的连接
*/
public DatabaseLink()throws IOException{//连接数据库
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException ex){
System.out.println(ex.getMessage());
}
try {
conn = DriverManager.getConnection("jdbc:odbc:comm","Administrator", "");
stmt = conn.createStatement();//创建一个 Statement 对象来将 SQL 语句发送到数据库。
} catch (SQLException ex1) {
System.out.println(ex1.getMessage());
}
}//end of the method DatabaseLink
/*//////////////数据库的关闭/////////////////////////////////*/
public void databaseclose(){
try{
if(stmt!=null){
stmt.close();
stmt=null;
}//end of if...
if(conn!=null){
conn.close();
conn=null;
}//end of if...
}catch(SQLException e){
}//end of try...catch...
}//end of the method databaseclose
///////////////////////数据库的查询///////////////////////////
public ResultSet query(String sql) {
ResultSet rs=null;
try{
if(conn==null){
DatabaseLink();
}//end of if...
rs=stmt.executeQuery(sql);
}catch (SQLException e){
}finally{
//databaseclose();
return rs;//返回sql语句的执行结果
}//end of try...catch...finally...
}//end of the method query(String)
//////////要查找的列名,表名,和条件组成查询语句/////////////////
public ResultSet query(String sColumn,String sTableName,String sCondition) {
ResultSet rs=null;
try{
if(conn==null){
DatabaseLink();
}//end of if....
rs=stmt.executeQuery(" select "+sColumn+" from "+sTableName+" where "+sCondition);
}catch (SQLException e){
}finally{
//databaseclose();
return rs;//返回sql语句的执行结果
}//end of try...catch...finally...
}//end of the method query(String,String,String)
////////////////根据表名 查找表名中的全部内容/////////////////////////////
public ResultSet queryAll(String sTableName){
ResultSet rs=null;
try{
if(conn==null){
DatabaseLink();
}//end of if...
rs=stmt.executeQuery(" select * from "+sTableName);
}catch(SQLException e){
}finally{
return rs;//返回sql语句的执行结果
}//end of try...catch...finally...
}//end of the method queryAll(String)
/*
根据表名和条件名 来查找表中的全部信息
*/
public ResultSet queryAll(String sTableName,String sCondition){
ResultSet rs;
try{
if(conn==null){
DatabaseLink();
}//end of if...
rs=stmt.executeQuery(" select * from "+sTableName+" where "+sCondition);
return rs;
}catch(SQLException e){
//System.err.println("192zxf查询:"+e.getMessage());
return null;
}//end of try...catch....
}//end of the method queryAll(String,String)
/*
///////////////数据库的插入///////////////////////////
*/
public boolean insert(String sql){
try{
if(stmt!=null){
stmt.close();
stmt=null;
}//end of if...
if(conn==null){
DatabaseLink();
}//end of if...
stmt=conn.createStatement();
int result=stmt.executeUpdate(sql);
if(result==0){
conn.rollback();
return false;
}//end of if...
conn.commit();
}catch(SQLException e){
return false;
}//end of try...catch....
return true;
}//end of the method insert(String)
/*////////////////////
根据表名,列名和列名值组成插入语句。插入一条记录
*/
public boolean insert(String sTable,String strRowName[],String[] strRowValue){
int iLength=strRowName.length;//要插入列的个数
String sql=" insert into "+sTable+" ( ";
for(int i=0;i<iLength-1;i++){
sql=sql+" "+strRowName[i]+",";
}//end of for...
sql=sql+strRowName[iLength-1]+") values (";
for(int i=0;i<iLength-1;i++){
sql=sql+" '"+strRowValue[i]+"',";
}//end of for...
sql=sql+"'"+strRowValue[iLength-1]+"')";
try{
if(stmt!=null){
stmt.close();
stmt=null;
}//end of if...
if(conn==null){
DatabaseLink();
}//end of if...
stmt=conn.createStatement();
int result=stmt.executeUpdate(sql);
if(result==0){
conn.rollback();
return false;
}//end of if...
conn.commit();
}catch(SQLException e){
return false;
}//end of try...catch....
return true;
}//end of the method insert(String)
/*
根据表名,列名和列名值组成插入语句。插入一批记 录
*/
public boolean insertMany(String sTable,String strRowName[],String[][] strRowValue){
int iNameLength=strRowName.length;//总共的表字段的个数
int iValueLength=strRowValue.length;//所插入的行数
String sql=" insert into "+sTable+" ( ";
for(int i=0;i<iNameLength-1;i++){
sql=sql+" "+strRowName[i]+",";
}//end of for...
sql=sql+strRowName[iNameLength-1]+") values (";
for(int j=0;j<iValueLength-1;j++){
for(int i=0;i<iNameLength-1;i++){
sql=sql+" '"+strRowValue[j][i]+"',";
}//end of for...
sql=sql+" '"+strRowValue[j][iNameLength-1] +"'), (";
}//end of outer for....
for(int i=0;i<iNameLength-1;i++){
sql=sql+" '"+strRowValue[iValueLength-1][i]+"',";
}//end of for...
sql=sql+" '"+strRowValue[iValueLength-1][iNameLength-1] +"')";
try{
if(stmt!=null){
stmt.close();
stmt=null;
}//end of if...
if(conn==null)
DatabaseLink();
stmt=conn.createStatement();
int result=stmt.executeUpdate(sql);
if(result==0){
conn.rollback();
return false;
}//end of if...
conn.commit();
}catch(SQLException e){
return false;
}//end of try...catch....
return true;
}//end of the method insertMany(String)
/*
////////////////////数据库记录的删除///////////////////////////
*/
//删除一项记录
public boolean delete(String sql){
try{
if(stmt!=null){
stmt.close();
stmt=null;
}//end of if...
if(conn==null){
DatabaseLink();
}//end of if...
stmt=conn.createStatement();
int result=stmt.executeUpdate(sql);
if(result==0){
conn.rollback();
return false;
}//end of if...
conn.commit();
}catch(SQLException e){
return false;
}//end of try...catch....
return true;
}//end of the method delete(String)
////////////根据表名和条件删除记录/////////////////////
public boolean delete(String sTable,String sCondition){
try{
if(stmt!=null){
stmt.close();
stmt=null;
}//end of if...
if(conn==null)
DatabaseLink();
stmt=conn.createStatement();
String sDelete="delete from"+sTable+" where "+sCondition;
int result=stmt.executeUpdate(sDelete);
if(result==0){
conn.rollback();
return false;
}//end of if...
conn.commit();
}catch(SQLException e){
return false;
}//end of try...catch....
return true;
}//end of the method delete(String,String,String)
///////////根据表名删除所有记录////////////////
public boolean deleteAll(String sTable){
try{
if(stmt!=null){
stmt.close();
stmt=null;
}//end of if...
if(conn==null){
DatabaseLink();
}//end of if...
stmt=conn.createStatement();
String sDelete=" delete from "+sTable;
int result=stmt.executeUpdate(sDelete);
if(result==0){
conn.rollback();
return false;
}//end of if....
conn.commit();
}catch(SQLException e){
return false;
}//end of try...catch..
return true;
}//end of the method deleteAll(String)
/*
//////////////更新数据库的记录///////////////////////
*/
public boolean update(String sql){
try{
if(stmt!=null){
stmt.close();
stmt=null;
}//end of if...
if(conn==null){
DatabaseLink();
}//end of if....
stmt=conn.createStatement();
int result=stmt.executeUpdate(sql);
if(result==0){
conn.rollback();
return false;
}//end of if....
conn.commit();
}catch(SQLException e){
return false;
}//end of try...catch....
return true;
}//end of the mehtod update(String)
/*
根据表名,条件,(查找列,旧的值,新的值)
*/
public boolean update(String sTable,String sColumn,String sOldColumn,String sNewColumn){
try{
if(stmt!=null){
stmt.close();
stmt=null;
}//end of if....
if(conn==null){
DatabaseLink();
}//end of if....
String sQuery=" select * from "+sTable+" where "+sColumn+"="+sOldColumn;
ResultSet rs=query(sQuery);
if(rs.next()){
rs.close();
String sUpdate="update "+sTable+" set "+sColumn+" ="+sOldColumn+" where "+sColumn+" ="+sNewColumn;
int result=stmt.executeUpdate(sUpdate);
if(result==0){
conn.rollback();
return false;
}else{
conn.commit();
}//end of if....
}else{
return false;
}//end of if....
}catch(SQLException e){
return false;
}//end of try...catch....
return true;
}//end of the method update(String,String,String)
}
TAG: