publicclassJdbcTest{privatestaticStringdriver="org.gjt.mm.mysql.Driver";privatestaticStringURL="jdbc:mysql://localhost:3306/test";privatestaticStringusername="root";priva...
public class JdbcTest {
private static String driver = "org.gjt.mm.mysql.Driver";
private static String URL = "jdbc:mysql://localhost:3306/test";
private static String username = "root";
private static String password = "123456";
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
try {//加载驱动
Class.forName(driver);
//创建连接
Connection con = DriverManager.getConnection(URL,username,password);
//创建连接上的statement
Statement stmt = con.createStatement();
//发起一个查询
ResultSet res = stmt.executeQuery("select * from userinfor");
//显示查询结果
while(res.next()){
int id = res.getInt("id");
String name = res.getString("name");
System.out.println(id+name);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
展开