日期:2014-05-17  浏览次数:20742 次

用JDBC访问数据库的问题
我想在打印出数据库web中的所有内容,代码如下(已添加jdbc驱动包)

  一个与数据库连接的javabean DBcon.java 
 
Java code

  package mypack;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBcon {
    
    public Connection conn=null;
    
    public Connection  getConnection()
    {
        
        String Driver="com.mysql.jdbc.Driver";
        String Url="jdbc:mysql://localhost:3306/test";
        //System.out.println("建立连接");
        
        try{
            Class.forName(Driver);        /* 加载驱动 */
                                          /* 连接数据库*/
            conn=DriverManager.getConnection( Url,"root","1234");
            //System.out.println("success");
            
        }catch(Exception e)
        {
            System.out.println("runtime");
            //throw new RuntimeException(e);
            e.printStackTrace();
        }
        return conn;
    }
    
    public static void main(String args[])
    {
        DBcon con=new DBcon();
        
        con.getConnection();
    }

}
  

一个用户属性的Javabean People.java
Java code

package mypack;

public class People {

    String name;
    String hometown;            //户籍
    String local;               //高校所在地
    String university;         //就读高校
    String highschool;          //曾读高中
    String major;              //专业
    String grade;             //年纪
    
    
    public void setName(String name)
    {
        this.name=name;
    }
    public String getName()
    {
        return name;
    }
    
    public void setHometown(String hometown)
    {
        this.hometown=hometown;
    }
    public String getHometown()
    {
        return hometown;
    }
    
    public void setLocal(String local)
    {
        this.local=local;
    }
    public String getLocal()
    {
        return local;
    }
    
    public void setUniversity(String university)
    {
        this.university=university;
    }
    public String getUniversity()
    {
        return university;
    }
    
    public void setHighschool(String highschool)
    {
        this.highschool=highschool;
    }
    public String getHighschool()
    {
        return highschool;
    }
    
    public void setMajor(String major)
    {
        this.major=major;
    }
    public String getMajor()
    {
        return major;
    }
    
    public void setGrade(String grade)
    {
        this.grade=grade;
    }
    public String getGrade()
    {
        return grade;
    }
}




一个访问数据库得到所有数据的javabean Getall.java
Java code

package mypack;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Getall {
    
    People[] sb;
    int num;
    public Getall()
    {
         num=0;
         sb= new People[100];
    }
    public void  loop()   
    {

         DBcon db=new DBcon();
         Connection conn=db.getConnection();
         PreparedStatement pstmt;
         String sql="select * from web ";
         ResultSet rs;
         
         try{
             pstmt=conn.prepareStatement(sql);
             rs=pstmt.executeQuery();
             
             while(rs.next()){
                 
                 sb[num].setName(rs.getString("name"));
                 sb[num].setHometown(rs.getString("hometown"));
                 sb[num].setLocal(rs.getString("local"));
                 sb[num].setUniversity(rs.getStri