java mysql 增删改查
时间: 2023-10-13 14:24:40 浏览: 39
1. Java MySQL插入数据
要将数据插入MySQL数据库,可以使用Java JDBC API的executeUpdate()方法。以下是一个简单的Java程序,可以将数据插入数据库:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class InsertData {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "");
// Execute a query
System.out.println("Inserting records into the table...");
stmt = conn.createStatement();
String sql = "INSERT INTO students (id, name, age) VALUES (1001, 'John Smith', 20)";
stmt.executeUpdate(sql);
System.out.println("Records inserted successfully...");
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
try {
if (stmt != null) {
conn.close();
}
} catch (SQLException se) {
} // do nothing
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
} // end finally try
} // end try
}
}
```
2. Java MySQL更新数据
要更新MySQL数据库中的数据,可以使用Java JDBC API的executeUpdate()方法。以下是一个简单的Java程序,可以更新数据库中的数据:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class UpdateData {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "");
// Execute a query
System.out.println("Updating records in the table...");
stmt = conn.createStatement();
String sql = "UPDATE students SET age = 21 WHERE id = 1001";
stmt.executeUpdate(sql);
System.out.println("Records updated successfully...");
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
try {
if (stmt != null) {
conn.close();
}
} catch (SQLException se) {
} // do nothing
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
} // end finally try
} // end try
}
}
```
3. Java MySQL删除数据
要从MySQL数据库中删除数据,可以使用Java JDBC API的executeUpdate()方法。以下是一个简单的Java程序,可以删除数据库中的数据:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DeleteData {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "");
// Execute a query
System.out.println("Deleting records from the table...");
stmt = conn.createStatement();
String sql = "DELETE FROM students WHERE id = 1001";
stmt.executeUpdate(sql);
System.out.println("Records deleted successfully...");
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
try {
if (stmt != null) {
conn.close();
}
} catch (SQLException se) {
} // do nothing
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
} // end finally try
} // end try
}
}
```
4. Java MySQL查询数据
要从MySQL数据库中查询数据,可以使用Java JDBC API的executeQuery()方法。以下是一个简单的Java程序,可以查询数据库中的数据:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SelectData {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "");
// Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "SELECT id, name, age FROM students";
ResultSet rs = stmt.executeQuery(sql);
// Extract data from result set
while (rs.next()) {
// Retrieve by column name
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
// Display values
System.out.print("ID: " + id);
System.out.print(", Name: " + name);
System.out.println(", Age: " + age);
}
rs.close();
System.out.println("Records selected successfully...");
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
try {
if (stmt != null) {
conn.close();
}
} catch (SQLException se) {
} // do nothing
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
} // end finally try
} // end try
}
}
```