引言Java作为一种广泛使用的编程语言,与MySQL数据库的结合使用为开发者提供了强大的数据管理能力。本文将详细介绍Java与MySQL连接的过程,包括环境搭建、代码实现、异常处理等关键步骤,帮助读者...
Java作为一种广泛使用的编程语言,与MySQL数据库的结合使用为开发者提供了强大的数据管理能力。本文将详细介绍Java与MySQL连接的过程,包括环境搭建、代码实现、异常处理等关键步骤,帮助读者轻松上手并高效实现数据交互。
pom.xml文件中添加JDBC驱动的依赖。 mysql mysql-connector-java 8.0.26
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; String username = "root"; String password = "password"; try { Connection conn = DriverManager.getConnection(url, username, password); System.out.println("连接成功"); } catch (SQLException e) { e.printStackTrace(); } }
}url:指定数据库的URL,包括IP地址、端口号、数据库名等。username:数据库用户名。password:数据库密码。useSSL:是否启用SSL加密,建议设置为false。serverTimezone:设置服务器时区,避免时间问题。import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Main { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; String username = "root"; String password = "password"; try (Connection conn = DriverManager.getConnection(url, username, password); PreparedStatement statement = conn.prepareStatement("SELECT * FROM users")) { ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { String name = resultSet.getString("name"); String age = resultSet.getString("age"); System.out.println("Name: " + name + ", Age: " + age); } } catch (SQLException e) { e.printStackTrace(); } }
}import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Main { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; String username = "root"; String password = "password"; try (Connection conn = DriverManager.getConnection(url, username, password); PreparedStatement statement = conn.prepareStatement("INSERT INTO users (name, age) VALUES (?, ?)")) { statement.setString(1, "张三"); statement.setInt(2, 20); int affectedRows = statement.executeUpdate(); if (affectedRows > 0) { System.out.println("插入成功"); } } catch (SQLException e) { e.printStackTrace(); } }
}import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Main { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; String username = "root"; String password = "password"; try (Connection conn = DriverManager.getConnection(url, username, password); PreparedStatement statement = conn.prepareStatement("UPDATE users SET age = ? WHERE name = ?")) { statement.setInt(1, 21); statement.setString(2, "张三"); int affectedRows = statement.executeUpdate(); if (affectedRows > 0) { System.out.println("更新成功"); } } catch (SQLException e) { e.printStackTrace(); } }
}import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Main { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; String username = "root"; String password = "password"; try (Connection conn = DriverManager.getConnection(url, username, password); PreparedStatement statement = conn.prepareStatement("DELETE FROM users WHERE name = ?")) { statement.setString(1, "张三"); int affectedRows = statement.executeUpdate(); if (affectedRows > 0) { System.out.println("删除成功"); } } catch (SQLException e) { e.printStackTrace(); } }
}在Java与MySQL连接过程中,可能会遇到各种异常。以下是一些常见的异常及其处理方法:
SQLException:数据库连接异常、SQL语句错误等。ClassNotFoundException:找不到JDBC驱动。SQLSyntaxErrorException:SQL语句语法错误。SQLIntegrityConstraintViolationException:违反了数据库完整性约束。try { // 尝试执行数据库操作
} catch (SQLException e) { // 处理SQLException异常 e.printStackTrace();
} catch (ClassNotFoundException e) { // 处理找不到JDBC驱动异常 e.printStackTrace();
} catch (Exception e) { // 处理其他异常 e.printStackTrace();
}本文详细介绍了Java与MySQL连接的过程,包括环境搭建、代码实现、异常处理等关键步骤。通过本文的学习,读者可以轻松上手并高效实现Java与MySQL的数据交互。希望本文对您的学习和工作有所帮助。