服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java、xml与数据库编程实践(四)

  [系列文章:java、xml与数据库编程实践(一)] 
  [系列文章:java、xml与数据库编程实践(二)] 
  [系列文章:java、xml与数据库编程实践(三)] 





 protected boolean alreadyin(string tname,string colname, string value)

    {      

        int result;

        resultset rst=null;

        try {

        //执行sql语句

        string query = "select "+colname+" from "+tname+" where "+colname+"='"+value+"'";

        statement statement = connformax.createstatement();

        rst = statement.executequery( query );

        if(rst.next())

        {

            statement.close();

            rst.close();

            return true;

        }      

     }

     catch ( sqlexception sqlex ) {

        sqlex.printstacktrace();

        return false;

     }

     return false;    

    }

 






    protected int getidfromnumber(string tname,string colname, string value)

    {      

        int result;

        resultset rst=null;

        try {

        connection conn= drivermanager.getconnection( destpara.geturl(), destpara.getusername(),destpara.getpassword());

        string query = "select id,"+colname+" from "+tname+" where "+colname+"='"+value+"'";

        system.out.println(query);

        statement statement = conn.createstatement();

        rst = statement.executequery( query );

        if(rst.next())

        {

            return rst.getint("id");

        } 

     }

     catch ( sqlexception sqlex ) {

        sqlex.printstacktrace();

        return 0;

     }

     return 0;    

    }

    /**

     * 得到某个表中的最大的id号

     */

 






    protected int getmax(string tname)

    {      

        int result;

        resultset rst=null;   

        try {

        //执行sql语句

        string query = "select max(id) from "+tname;

        statement statement = connformax.createstatement();

        rst = statement.executequery( query );

        if(rst.next())

        {

            return rst.getint(1)+1;

        }

     }

     catch ( sqlexception sqlex ) {

        sqlex.printstacktrace();

        return 0;

     }

     return 1;    

    }

    /**

     * 执行某一段sql语句

     */

    public static void execute(connpara connpara,string stmt) throws sqlexception

 






    {

        connection conn=null;

        preparedstatement ps = null;

        try {

            conn=drivermanager.getconnection( connpara.geturl(),  connpara.getusername(), connpara.getpassword());

            system.out.println(stmt);

            ps = conn.preparestatement(stmt);

            ps.executeupdate();

        } catch (exception e) {

            e.printstacktrace();

            system.out.println(e.getmessage());

        } finally {

        if (ps!=null) ps.close();

        if (conn!=null)conn.close();

        }

    }

    public static void main(string argc[])

    {

        dbinput copydb=new dbinput();

        copydb.dbinit();

        copydb.copyproduct();  

    }  

}

  问题:

  1) access数据库不能直接由jdbc读写,解决办法是先把access配置在odbc中,然后再通过odbc来操作access数据库。

  2) 执行时找不到com.microsoft.jdbc.sqlserver.sqlserverdriver类,这是因为在运行这个类时,要下载微软的jdbc包,这个包中有三个文件: msbase.jar,mssqlserver.jar,msutil.jar,把这三个文件包含进去,就不会有问题了。

 





扫描关注微信公众号