21xrx.com
2025-07-07 11:33:06 Monday
文章检索 我的文章 写文章
Java访问数据库:使用JDBC操作数据库的完整教程
2023-06-14 21:55:11 深夜i     12     0
Java JDBC MySQL

Java作为一门面向对象的编程语言,可以轻松地访问和操纵数据库。在本教程中,我们将介绍如何使用Java Database Connectivity(JDBC)API来操作数据库。首先,我们将学习如何使用JDBC连接数据库,并执行完整的CRUD操作。接下来,我们将给出一个代码案例来说明如何连接一个MySQL数据库,以及如何执行插入、查询、删除和更新操作。

连接到数据库

要连接到数据库,我们需要通过使用标准的JDBC API驱动程序来创建一个连接对象。以下代码展示了如何连接到MySQL数据库:

import java.sql.*;
public class MySQLConnection {
  public static void main(String[] args) {
  Connection conn = null;
  try {
   String url = "jdbc:mysql://localhost:3306/mydatabase";
   String username = "user";
   String password = "password";
   Class.forName("com.mysql.jdbc.Driver");
   conn = DriverManager.getConnection(url, username, password);
   System.out.println("Connected to the database");
  } catch (SQLException e) {
   System.out.println("Oops, something went wrong!");
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } finally {
   try {
     if (conn != null) {
      conn.close();
      System.out.println("Disconnected from database");
     }
   } catch (SQLException ex) {
     ex.printStackTrace();
   }
  }
}
}

这段代码会将我们连接到名为“mydatabase”的MySQL数据库。在连接成功后,我们将输出“Connected to the database”,并在最后关闭连接。

执行CRUD操作

接下来,我们将展示如何执行CRUD操作,即插入、查询、删除和更新。

插入

首先,我们将展示如何将数据插入到数据库表中。以下代码展示了如何将数据插入到名为“customers”的表中:

import java.sql.*;
public class MySQLInsert {
  public static void main(String[] args) {
  Connection conn = null;
  Statement stmt = null;
  try {
   String url = "jdbc:mysql://localhost:3306/mydatabase";
   String username = "user";
   String password = "password";
   Class.forName("com.mysql.jdbc.Driver");
   conn = DriverManager.getConnection(url, username, password);
   String sql = "INSERT INTO customers " +
          "VALUES (100, 'John Smith', '15 Main St', 'New York', 'NY')";
   stmt = conn.createStatement();
   stmt.executeUpdate(sql);
   System.out.println("Inserted record into the table");
  } catch (SQLException e) {
   System.out.println("Oops, something went wrong!");
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } finally {
   try {
     if (stmt != null) {
      stmt.close();
     }
   } catch (SQLException ex) {
     ex.printStackTrace();
   }
   try {
     if (conn != null) {
      conn.close();
      System.out.println("Disconnected from database");
     }
   } catch (SQLException ex) {
     ex.printStackTrace();
   }
  }
}
}

查询

以下代码展示了如何从名为“customers”的表中查询数据:

import java.sql.*;
public class MySQLSelect {
  public static void main(String[] args) {
  Connection conn = null;
  Statement stmt = null;
  ResultSet rs = null;
  try {
   String url = "jdbc:mysql://localhost:3306/mydatabase";
   String username = "user";
   String password = "password";
   Class.forName("com.mysql.jdbc.Driver");
   conn = DriverManager.getConnection(url, username, password);
   String sql = "SELECT id, name, address, city, state FROM customers";
   stmt = conn.createStatement();
   rs = stmt.executeQuery(sql);
   while(rs.next()){
     int id = rs.getInt("id");
     String name = rs.getString("name");
     String address = rs.getString("address");
     String city = rs.getString("city");
     String state = rs.getString("state");
     //Display values
     System.out.print("ID: " + id);
     System.out.print(", Name: " + name);
     System.out.print(", Address: " + address);
     System.out.print(", City: " + city);
     System.out.println(", State: " + state);
   }
  } catch (SQLException e) {
   System.out.println("Oops, something went wrong!");
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } finally {
   try {
     if (rs != null) {
      rs.close();}
   } catch (SQLException ex) {
     ex.printStackTrace();
   }
   try {
     if (stmt != null) {
      stmt.close();}
   } catch (SQLException ex) {
     ex.printStackTrace();
   }
   try {
     if (conn != null) {
      conn.close();
      System.out.println("Disconnected from database");
     }
   } catch (SQLException ex) {
     ex.printStackTrace();
   }
  }
}
}

删除

以下代码展示了如何从名为“customers”的表中删除行:

import java.sql.*;
public class MySQLDelete {
  public static void main(String[] args) {
  Connection conn = null;
  Statement stmt = null;
  try {
   String url = "jdbc:mysql://localhost:3306/mydatabase";
   String username = "user";
   String password = "password";
   Class.forName("com.mysql.jdbc.Driver");
   conn = DriverManager.getConnection(url, username, password);
   String sql = "DELETE FROM customers WHERE id = 100";
   stmt = conn.createStatement();
   stmt.executeUpdate(sql);
   System.out.println("Deleted record from the table");
  } catch (SQLException e) {
   System.out.println("Oops, something went wrong!");
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } finally {
   try {
     if (stmt != null) {
      stmt.close();
     }
   } catch (SQLException ex) {
     ex.printStackTrace();
   }
   try {
     if (conn != null) {
      conn.close();
      System.out.println("Disconnected from database");
     }
   } catch (SQLException ex) {
     ex.printStackTrace();
   }
  }
}
}

更新

最后,以下代码展示了如何更新名为“customers”的表中的数据:

import java.sql.*;
public class MySQLUpdate {
  public static void main(String[] args) {
  Connection conn = null;
  Statement stmt = null;
  try {
   String url = "jdbc:mysql://localhost:3306/mydatabase";
   String username = "user";
   String password = "password";
   Class.forName("com.mysql.jdbc.Driver");
   conn = DriverManager.getConnection(url, username, password);
   String sql = "UPDATE customers SET name = 'John Doe' WHERE id = 1";
   stmt = conn.createStatement();
   stmt.executeUpdate(sql);
   System.out.println("Updated record in the table");
  } catch (SQLException e) {
   System.out.println("Oops, something went wrong!");
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } finally {
   try {
     if (stmt != null) {
      stmt.close();
     }
   } catch (SQLException ex) {
     ex.printStackTrace();
   }
   try {
     if (conn != null) {
      conn.close();
      System.out.println("Disconnected from database");
     }
   } catch (SQLException ex) {
     ex.printStackTrace();
   }
  }
}
}

  
  

评论区