jdbc连接mysql并执行简单的CRUD的步骤:
1.注册驱动(需要抛出/捕获异常)
Class.forName("com.mysql.jdbc.Driver");
2.建立连接需要抛出/捕获异常)
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "Danny2036");
3.创建statement
statement = connection.createStatement();
4.创建sql语句
String sql = "insert into user values(10, \"noname\", 31)";
5.执行sql语句,如果是查询需要用ResultSet接收结果集
int affectrows = statement.executeUpdate(sql); System.out.println(affectrows);
6.关闭资源
if (statement != null) { statement.close(); } if (connection != null) { connection.close(); }
————————————————————————分割线——————————————————————————————————
最后贴出全部代码(包括insert,selectOne和selectAll)
package com.colin.dao; import java.sql.*; public class JdbcTest { /** * 插入语句 * @param id 主键id int * @param name 姓名 String * @param age 年龄 int */ public static void update(int id, String name, int age) throws SQLException { long starttime = System.currentTimeMillis(); Connection connection = null; Statement statement = null; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/testdb", "root", "Danny2036"); statement = connection.createStatement(); String sql = "update user set name = \'" + name + "\', age = "+ age +" where id = " + id; System.out.println(sql); int affectrows = statement.executeUpdate(sql); System.out.println(affectrows); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { if (connection != null) { connection.close(); } if (statement != null) { statement.close(); } } System.out.println(System.currentTimeMillis() - starttime); } public static void selectOne(int id) throws ClassNotFoundException, SQLException { Statement statement = null; Connection connection = null; try { // 注册驱动 Class.forName("com.mysql.jdbc.Driver"); // 建立连接 connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/testdb", "root", "Danny2036" ); // 创建statement statement = connection.createStatement(); // 定义sql语句 String sql = "SELECT id, name, age FROM user where id = " + id; // 执行sql语句并获得结果 ResultSet resultSet = statement.executeQuery(sql); // 遍历结果集 while (resultSet.next()) { int idResult = (int) resultSet.getInt("id"); String nameResult = (String) resultSet.getString("name"); int ageResult = (int) resultSet.getInt("age"); System.out.println(idResult + "\t" + nameResult + "\t" + ageResult); } } finally { if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } } public static void selectAll() throws ClassNotFoundException, SQLException { Statement statement = null; Connection connection = null; try { // 注册驱动 Class.forName("com.mysql.jdbc.Driver"); // 建立连接 connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/testdb", "root", "Danny2036" ); // 创建statement statement = connection.createStatement(); // 定义sql语句 String sql = "SELECT id, name, age FROM user"; // 执行sql语句并获得结果 ResultSet resultSet = statement.executeQuery(sql); // 遍历结果集 while (resultSet.next()) { int idResult = (int) resultSet.getInt("id"); String nameResult = (String) resultSet.getString("name"); int ageResult = (int) resultSet.getInt("age"); System.out.println(idResult + "\t" + nameResult + "\t" + ageResult); } } finally { if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } } public static void main(String[] args) throws SQLException, ClassNotFoundException { selectAll(); } /* // 向上抛异常的方式 public static void main(String[] args) throws ClassNotFoundException, SQLException { long starttime = System.currentTimeMillis(); Connection connection = null; Statement statement = null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //建立连接 connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/testdb", "root", "Danny2036"); //创建statement statement = connection.createStatement(); //定义sql String sql = "insert into user values(10, \"noname\", 31)"; //执行sql,如果是查询需返回结果集 int affectrows = statement.executeUpdate(sql); System.out.println(affectrows); } finally { //关闭资源 if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } System.out.println("总执行时间: " + (System.currentTimeMillis() - starttime)); } */ /* // 捕获异常的方式 public static void main(String[] args) throws SQLException { long starttime = System.currentTimeMillis(); Statement statement = null; Connection connection = null; try { // 注册驱动 Class.forName("com.mysql.jdbc.Driver"); // 建立连接 connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/testdb?useSSL=true", "root", "Danny2036"); // 创建statement statement = connection.createStatement(); // 定义sql String sql = "insert into user values(9, \"ahaha\", 22)"; // 执行sql,如果是查询,遍历结果集 int affectrows = statement.executeUpdate(sql); System.out.println(affectrows); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭资源 if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } System.out.println("总用时 : " + (System.currentTimeMillis() - starttime)); } */ }
——————————————————**********最后附上bean代码***********————————————————————
package com.colin.bean; public class User { private int id; private String name; private int age; public User(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public User(String name, int age) { this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }