java
链接数据库 mysql
首先将mysql的链接jar包,创建路径
1.创建驱动 Class.forName("com.mysql.jdbc.Driver");
2。创建数据库的链接Connection con=DriverManager.getConnection();
3. 写sql语句,, 创建Statement对象 Statement sm=con.creatStatement();
4.创建结果集ResultSet rs=sm.execute(sql);
5.关闭连接和结果集
Class.forName("com.mysql.jdbc.Driver");
Connection connection=DriverManager.getConnection(url, user, password);
String sql="SELECT * FROM user";
Statement st=connection.createStatement();
ResultSet rs=st.executeQuery(sql);
while (rs.next()){
int id=rs.getInt(1);
String name=rs.getString(2);
int age=rs.getInt(3);
System.out.println(id+" -----"+name+"-----"+age);
}
rs.close();
connection.close();