자바 test 표 에서 데이터 조회

5823 단어
 1 package JDBCDEMOS;
 2 
 3 //import java.net.ConnectException;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Connection;
 8 import java.sql.Statement;
 9 
10 //import com.mysql.jdbc.Statement;
11 
12 public class JDBCDEMO1 {
13     public static final String DBDRIVER = "com.mysql.jdbc.Driver";
14     public static final String DBURL = "jdbc:mysql://localhost:3306/test";
15     public static final String DBUSER = "root";
16     public static final String DBPASS = "123123";
17 
18     public static void main(String[] args) throws Exception {
19         Connection conn = null;//      
20         Statement stmt = null;
21         ResultSet rs=null;
22         String sql = "SELECT *FROM mytab";
23         Class.forName(DBDRIVER);
24         conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
25         stmt = conn.createStatement();
26         rs=stmt.executeQuery(sql);
27         while(rs.next()){
28             int id=rs.getInt("id");
29             String name=rs.getString("name");
30             int age=rs.getInt("age");
31             System.out.print("  : "+id+";");
32             System.out.print("  : "+name+";");
33             System.out.println("  : "+age+";");
34             System.out.println("---------------------");
35             
36         }
37         rs.close();
38         stmt.close();
39         conn.close();
40     }
41 }

좋은 웹페이지 즐겨찾기