Javaweb中的JDBC使用步骤
1、导入jar包
2、注册驱动
Class.forName("com.jdbc.mysql.Driver");
3、获取数据库连接
String url="JDBC:mysql://127.0.0.1:3306/databaseName";
String username="root";
password="123456";
Connection coon=DriverManager.getconnection(url,username,password);
4、定义mysql语句
String sql_1=“insert into table_name(id,name,password) values(1,"张三",“123”)”;
String sql_2="select from table_name where id=5";
5、获取Mysql对象
Statement stat=conn.creatStatement();
6、执行Mysql语句
int rows=stat.executeUpdate(sql_1);
ResultSet rs = stmt.executeQuery(sql_2);
7、处理结果
if(rows>0){
System.out.printf("修改成功!");
}
while(rs.next()){
System.out.println(“用户名:”+rs.getString("user")+" 密码:"+rs.getString(“password”));
}
8、释放资源(最先打开最后释放)
stat.close();
conn.close();
rs.close();