JAVA連接MySQL

 

String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test";

try {
Class.forName(driver);
Connection conn=DriverManager.getConnection(url,"root","3.14");
PreparedStatement pstmt=conn.prepareStatement("select * from person");
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString("username"));
}
rs.close();
pstmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
posted @ 2012-03-23 23:41  蕭慶蘋  阅读(126)  评论(0编辑  收藏  举报