1. 程式人生 > >jdbc筆記(一) 使用Statement對單表的CRUD操作

jdbc筆記(一) 使用Statement對單表的CRUD操作

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 +
                '}';
    }
}