| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 607 人关注过本帖
标题:求解java问题
取消只看楼主 加入收藏
顾帆
Rank: 1
等 级:新手上路
帖 子:10
专家分:2
注 册:2011-11-12
结帖率:60%
收藏
已结贴  问题点数:16 回复次数:1 
求解java问题
package common;

import
import
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;


//import com.hxtt.global.v;


public class SMADao_up
{
    private String className;
    private String url;
    //private String username;
    //private String password;
    private String id;
    private String name;
    private Connection con_up;
    private Connection con;
    private PreparedStatement pstm;
    private ResultSet rs;
    private Statement st;
    public SMADao_up()
    {
        try{
            className="com.microsoft.sqlserver.jdbc.SQLServerDriver";
            Class.forName("com.hxtt.sql.access.AccessDriver");
            //String str= "jdbc:access:/c:/database.mdb";
            url="jdbc:access:/gbkh.mdb";
        }catch(ClassNotFoundException e){
            System.out.println("加载数据库驱动程序失败!");
            e.printStackTrace();
        }
    }
    public void getCon(){
        try {
            con_up=DriverManager.getConnection(url);
            con_up.setAutoCommit(false);
        } catch (SQLException e) {
            System.out.println("获取数据库连接失败!");
            e.printStackTrace();
        }
    }
    public void getCon_normal()
    {
        try {
            con=DriverManager.getConnection(url,id,name);
            con.setAutoCommit(false);
        } catch (SQLException e) {
            System.out.println("获取数据库连接失败!");
            e.printStackTrace();
        }
    }
   
    /////////////////////////////////////////////////////////////////////////


      
        
        /**
         * 根据sql语句从数据库中取出结果集
         * 并封装成二维Vector向量
         * @param sql
         * @return
         */
    public Vector<Vector<TableValue>> DBQuery(String sql)
    {
        Vector<Vector<TableValue>> v = new Vector<Vector<TableValue>>();
        Connection con = null;
        try {
                    con=DriverManager.getConnection(url);
                    con.setAutoCommit(false);
                } catch (Exception e) {
                    System.out.println("");
                    e.printStackTrace();
                }
                ResultSet rs = null;
            Statement st = null;
                System.out.println(con == null);
        try
        {
            st = con.createStatement();
            rs = st.executeQuery(sql);
                        ResultSetMetaData rsmd=rs.getMetaData();
            int i = rsmd.getColumnCount();
                        while(rs.next())
                        {
                                Vector<TableValue> vx = new     Vector<TableValue>();
                             
                                for(int m=1;m<=i;m++)
                                {
                                        vx.add(rs.getObject(m));
                                }
                                vx.add(v);
                        }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                st.close();
                con.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        return v;
    }
    /**
     * 插入,删除,添加都可以使用此方法
     * @param sql
     * @return
     */
    public int upData(String sql)
        {
            int count = 0;
            Connection con = null;
             try {
        con=DriverManager.getConnection(url);
        con.setAutoCommit(false);
                //设置事务隔离级别为读已提交
                con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            } catch (Exception e) {
        System.out.println("");
        e.printStackTrace();
            }
        Statement st = null;
            try{
                st = con.createStatement();
                count = st.executeUpdate(sql);
                if(count>0)
                    ();
            }catch(Exception e){
                try{
                    con.rollback();
                }catch(Exception ee){
                    ee.printStackTrace();
                }
                e.printStackTrace();
            }
            finally{
        try{
                    st.close();
                    con.close();
        }catch(Exception e){
                    e.printStackTrace();
        }
            }
            return count;
        }
   
    public static void main(String[] args){
        SMADao_up dao = new SMADao_up();
        File f = new File(".");
        System.out.println(f.getAbsolutePath());
        Vector<Vector<TableValue>> v = dao.DBQuery("select * from 部门");
        //System.out.println(v);
         //System.out.println("数据库连接成功");
        for(int i=0;i<v.size();i++){
            for(int j=0;j<v.elementAt(i).size();j++){
                System.out.print(v.elementAt(i).elementAt(j));
            }
            System.out.println();
        
        }
    }
}


为什么我的TableValue会报错
我不明白是什么原因
搜索更多相关主题的帖子: class java password private package 
2011-12-16 17:36
顾帆
Rank: 1
等 级:新手上路
帖 子:10
专家分:2
注 册:2011-11-12
收藏
得分:0 
呵呵我解决了
2011-12-17 12:42
快速回复:求解java问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029303 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved