jdbc连接数据库的步骤

网传已久的:贾琏欲执事,即:加载驱动,建立连接,创建语句,执行语句,释放资源

1、加载驱动

class.forName("com.mysql.jdbc.Driver");

2、建立连接

Connection conn = DriverManager.getConnection(url,user,password);

3、创建语句

PreparedStatement ps = conn.prepareStatement("select * from employee where id  = ?");

ps.setInt(5);

4、执行语句

//执行SQL语句,查询。返回ResultSet对象(结果集)

Result rs = ps.executeQuery();

//executeUpdate(sql);
// 执行DDL/DML语句,执行DML返回受影响的行数,DDL返回0

5、释放资源

finally{

  if(conn!=null){

    conn.close();

  }

}

原文地址:https://www.cnblogs.com/xiaoxli/p/9453051.html