先添加jdbc jar包,测试代码如下:
package lee;
import java.sql.*;
class ConnMySql {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/test",
"root","123456");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from users");
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t"
+rs.getString(2) + "\t"
+rs.getString(3) );
}
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}