日期:2014-05-18  浏览次数:20596 次

java一个链接数据库的类里一个实例不能调用两个方法
我必须用两个实例分别调用两个方法才行,一个实例不能调用两个方法,关于数据库链接的,是不是一些变量定义有问题
这是构造函数
public JDBCTest(){
String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://127.0.0.1:3306/car";
        String user = "root";
        String password = "root";

        try
        {
            Class.forName(driver);
            this.conn = DriverManager.getConnection(url, user, password);
            if(!this.conn.isClosed())
               System.out.printf("");
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
}


这是两个方法
public 	float  getCO2(){
try
        {
String sql="select co2 from co2 where id=(select max(id) from co2)";
Statement statement = this.conn.createStatement();
        ResultSet rs = statement.executeQuery(sql);
        float i=0;
        while(rs.next())
        i=rs.getFloat("co2");
        rs.close();
        this.conn.close();
        return i;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
return 0;
}
public  float  get