1
加载特定的jdbc驱动库:如OJDBC 对于oracle数据库的驱动(事先要下载好)
2:
通过DriverManager类的getConnection连接数据库,返回Connection对象
3
根据Connection对象的createStatement返回Statement对象来执行SQL语句
注意
Statement statement=connection.createStatement()最好是用PreparedStatement pstmt=connection.PreparedStatement(sql);代替
当然事先要定义好sql 如: String sql=“insert into user (name,sex)values(?,?);问号是站位作用
这时pstmt.setString(”张三“,”男“);
pstmt.executeUpdate();
执行完sql语句释放资源:
pstmt.close();
connectio.close();
当然用了preparedStatement就不用第四部了
4
根据Statement的executeQuery执行sql语句,返回ResultSet或者executeUpdate,前者是执行查询语句的,后者是insert和update