日期:2014-05-17 浏览次数:20742 次
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(); } }
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; } }
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