注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 利用手机和电池实现反地心..
 帮助

一个数据库连接的class


2008-05-09 08:22:01
 标签:Java   [推送到技术圈]

PHP 代码:
package com.ljsilver.util;
import java.sql.*;

public class 
DatabaseOperate{
    private 
String Database_host "localhost:3306";
    private 
String Database_dsn "mydb";
    private 
String Database_user "user";
    private 
String Database_pswd "password";
    private 
Connection con null;
    private 
PreparedStatement ps null;
    private 
ResultSet result null;
    private 
boolean dirty false;
    private 
String sqlStr;
    private 
String url "jdbc:mysql://"+Database_host+"/"+Database_dsn+"?user="
        
+Database_user+"&password="+Database_pswd
        
+"&useUnicode=true&characterEncoding=GBK";

    public 
DatabaseOperate(String sqlStr){
        
this.sqlStr =sqlStr;
        
buildConnection();
    }

    public 
DatabaseOperate(){
    }

    public 
void buildConnection(){
        try{
            Class.
forName("org.gjt.mm.mysql.Driver").newInstance();
            
con DriverManager.getConnection(url);
            
ps=con.prepareStatement(this.sqlStr,
                                        
ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        
ResultSet.CONCUR_READ_ONLY);
        }
        catch(
Exception e){
            
System.out.println(e.toString());
        }
    }

    public 
void setSqlStr(String s){
        
this.sqlStr s;
    }

    public 
String getSqlStr(){
        return 
this.sqlStr;
    }

    public 
void setString(int n,String sqlStringthrows  SQLException{
        
ps.setString(n,sqlString);
    }

    public 
void setInt(int n,int sqlIntthrows  SQLException{
        
ps.setInt(n,sqlInt);
    }

        public 
void setBoolean(int iboolean flagthrows SQLException{
          
ps.setBoolean(iflag);
        }

        public 
void setDate(int iDate datethrows SQLException{
          
ps.setDate(idate);
        }

        public 
void setLong(int ilong lthrows SQLException{
          
ps.setLong(il);
        }

        public 
void setFloat(int ifloat fthrows SQLException{
          
ps.setFloat(if);
        }

        public 
void setBytes(int ibyte abyte0[]) throws SQLException{
          
ps.setBytes(iabyte0);
        }

    public 
ResultSet getResultSet(){
        try{
            
this.result ps.executeQuery();
        }catch(
SQLException sqlex){
            
System.out.println(sqlex.toString());
        }
        return 
this.result;
    }

    public 
void executeSql(){
        try{
            
ps.executeUpdate();
            
dirty true;
        }catch(
SQLException sqlex){
            
System.out.println(sqlex.toString());
        }
    }

    public 
boolean getDirty(){
        return 
this.dirty;
    }

    public 
void close(){
        try{
            if(
result!=null){
                
result.close();
                
result=null;
            }
            if(
ps!=null){
                
ps.close();
                
ps=null;
            }
            if(
con!=null){
                
con.close();
                
con=null;
            }
        }catch(
SQLException sqlex){
            
System.out.println(sqlex.toString());
        }
    }









    文章评论
 
 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: