linux 笔记: mysql : connect with jdbc

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 import java.sql.Statement;
 6 
 7 
 8 public class Hello {
 9 
10     /**
11      * @param args
12      * @throws ClassNotFoundException 
13      * @throws SQLException 
14      */
15     public static void main(String[] args) throws ClassNotFoundException, SQLException {
16         Class.forName("com.mysql.jdbc.Driver"); 
17         String connectStr = "jdbc:mysql://localhost:3306/testdb?user=root&password=*****&useUnicode=true&characterEncoding=UTF8";
18         Connection conn = DriverManager.getConnection(connectStr);
19         Statement stat = conn.createStatement();
20         ResultSet rs=stat.executeQuery("select * from `ab c1`");
21         while(rs.next()) {
22             System.out.println(rs.getObject(1).toString());
23         }
24         rs.close();
25         conn.close();
26     }
27 
28 }

posted on 2012-03-26 00:22  learner  阅读(254)  评论(0)    收藏  举报

导航