Java操作数据库主要有以下三个步骤:
1)查找数据库驱动;
2)建立数据库连接,并生成相应的连接对象Connection;
3)生成数据库操作对象stmtStatement,然后使用Statement对象进行数据库的增删改查操作。
注意:
1.增删改操作,使用executeUpdate()或者execute()提交,返回值为boolean类型。
2.查询操作,使用executeQuery(),返回为记录集ResultSet。
3.关闭时先关闭Statement对象再关闭Connection对象。
一、一类到底(以insert为例)
public class DBProces{
public static void main(String[] args) {
Connection conn = null;
//使用PreparedStatement防止sql注入
PreparedStatement st = null;
boolean rs = false;
String url = "jdbc:mysql://localhost:3306/db11?serverTimezone=UTC";
String userName = "root";
String password = "zxc051