Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>JDBC>>一个封装了基本JDBC操作的类
一个封装了基本JDBC操作的类
2005-06-08   来源:JAVA家  作者:JAVA家
odbc.java
---------------------------------------------

package bbs;

/*
database operation class, test by odbc

This javabean is written by zergling
It is my first javabean :o
version 1.01
*/

import java.sql.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import sun.io.*;

public class odbc
    {
    Connection sqlCon;
    ResultSet rstSql;
    Statement stmS;
    String strCon;
    String strSql;
    boolean status;
    long rowcount;
    int page;
    int pagesize;
    long pagecount;
    long firstrecord;

    //connect to the default database
    public boolean connect()
        {
        //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        this.strCon = "jdbc:odbc:jspbbs";                                                  //replace with your default database
        try
            {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            this.sqlCon = java.sql.DriverManager.getConnection(this.strCon,"sa","");    //replace with your default database connection configure option
            this.status = true;
            return true;
            }
        catch(Exception e)
            {
            this.status = false;
            return false;
            }
        }

    //connect to the custom database
    public boolean connect(String conName,String username,String password)
        {
        //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        this.strCon = conName;                                                  
        try
            {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            this.sqlCon = java.sql.DriverManager.getConnection(this.strCon,username,password);    
            this.status = true;
            return true;
            }
        catch(Exception e)
            {
            this.status = false;
            return false;
            }
        }

    //execute sql(insert,update,delete,...)
    public boolean execute(String s)
        {
        try
            {
            this.stmS = this.sqlCon.createStatement();
            this.stmS.executeUpdate(s);
            this.status = true;
            return true;
            }
        catch(Exception e)
            {
            this.status = false;
            return false;
            }
        }

    //query the data from database
    public boolean query(String s)
        {
        try
            {
            this.rowcount = 0;
            this.stmS = this.sqlCon.createStatement();
            this.rstSql = this.stmS.executeQuery(s);
            while (this.nextrecord())
                {
                this.rowcount++;
                }
            this.rstSql = this.stmS.executeQuery(s);
            this.status = true;
            return true;
            }
        catch(Exception e)
            {
            this.status = false;
            return false;
            }
        }
    
    //return the row count
    public long getrowcount()
        {
        return this.rowcount;
        }

    //return the pagecount
    public long getpagecount()
        {
        return this.pagecount;
        }
    
    //return the resultset of data
    public String getstring(String s)
        {
        try
            {
            return this.rstSql.getString(s);
            }
        catch(Exception e)
            {
            return "not exists";
            }
        }

    public int getint(String s)
        {
        try
            {
            return Integer.parseInt(this.rstSql.getString(s));
            }
        catch(Exception e)
            {
            return 0;
            }
        }

    //resultset move forward
    public boolean nextrecord()
        {
        try
            {
            return this.rstSql.next();
            }
        catch(Exception e)
            {
            return false;
            }
        }
    
    //set current page (recall first)
    public boolean setpage(int size,int no)
        {
        this.pagesize = size;
        this.page = no;
        this.pagecount = Math.round((this.rowcount - 1) / this.pagesize)+1; 
        this.firstrecord = this.pagesize * ( this.page - 1 );
        try
            {
            for(int i=0;i<this.firstrecord;i++)
                if (!this.nextrecord()) break;
            return true;
            }
        catch(Exception e)
            {
            return false;
            }
        }
    
    //get string from database and change it into chinese
    public String readChinese(String s)
        {
        try
            {
            String temp = new String(s.getBytes("GB2312"),"8859_1");
            return temp;
            }
        catch(Exception e)
            {
            return s;
            }
        }

    //write string to the database"s chinese transform
    public static String writeChinese(String s)
        {
        char[] orig =s.toCharArray();
        byte[] dest =new byte[orig.length];
        for(int i=0;i<orig.length;i++)
        dest[i] =(byte)(orig[i]&0xFF);
        try
            {

            ByteToCharConverter toChar =ByteToCharConverter.getConverter("gb2312");

            return new String(toChar.convertAll(dest));
            }
        catch(Exception e)
            {
            return s;
            }
        }


    //string"s search and replace
    public String replace(String con ,String tag,String rep){

        int j=0;

        int i=0;

        int k=0;

        String RETU="";

        String temp =con;

        int tagc =tag.length();

        while(i<con.length()){

            if(con.substring(i).startsWith(tag)){

                temp =con.substring(j,i)+rep;

                RETU+= temp;

                i+=tagc;

                j=i;

            }

            else{

                i+=1;

            }

            

        }

        RETU +=con.substring(j);

        return RETU;

    }    
    
        public Vector listValue(String con ,String tag){

        int j=0;

        int i=0;

        int k=0;

        Vector vv=new Vector();

        String temp =con;

        int tagc =tag.length();

        while(i<con.length()){

            if(con.substring(i).startsWith(tag)){

                temp =con.substring(j,i);

                vv.addElement(temp);

                i+=tagc;

                j=i;

            }

            else{

                i+=1;

            }

            

        }

        vv.addElement(con.substring(j));

        return vv;

    }    


    //filt the html code & sql symbol
    public String htmlencode(String s)
        {
        try
            {
            String temp = this.replace(s,"<","<");
            temp = this.replace(temp,">",">");
            temp = this.replace(temp,""",""");
            temp = this.replace(temp,""",""");
            temp = this.replace(temp," ","&nbsp;");
            temp = this.replace(temp,"
","<br>");
            return temp;
            }
        catch(Exception e)
            {
            return s;
            }
        }
    
    //return the status of last operation
    public boolean getstatus()
        {

        return this.status;
        }
    
    //close all object
    public boolean close()
        {
        try
            {
            if (this.sqlCon != null) this.sqlCon.close();
            if (this.rstSql != null) this.rstSql.close();
            if (this.stmS != null) this.stmS.close();
            this.status = true;
            return false;
            }
        catch(Exception e)
            {
            this.status = false;
            return true;
            }
        }

    }
  --相关文章--
· 通过JDBC连接oracle数据库的十大技巧 (2007-04-13)
· 第一个JDBC程序 (2007-04-13)
· 符合oo惯例的表现层控制 (2007-04-13)
· 用hibernate对递归数据的操作 (2007-04-13)
· 煮酒论英雄nbsp;-nbsp;漫谈Java数据库存取技术 (2007-04-13)
· 利用weblogic的数据源作为hibernate的数据源的例子 (2007-04-13)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接