下面是详细讲解Java连接MySQL数据库并进行内容查询的完整攻略:
前置条件
- Java编程环境已经安装并配置好;
- MySQL数据库已经安装并设置好用户名和密码;
- MySQL JDBC驱动程序已经下载,并在项目中添加了相关jar包。
连接MySQL数据库
首先,我们需要在Java代码中连接MySQL数据库。这可以通过以下代码完成:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectMySQL {
public static void main(String[] args) {
Connection conn = null;
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC";
String user = "your_username";
String password = "your_password";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
System.out.println("Database connection established");
} catch (ClassNotFoundException | SQLException e) {
System.err.println("Cannot connect to database server");
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
System.out.println("Database connection terminated");
} catch (SQLException e) { /* ignored */}
}
}
}
}
其中:
driver
:MySQL JDBC驱动程序的类名,可以直接使用com.mysql.cj.jdbc.Driver;url
:通过JDBC连接到MySQL数据库的URL,其中localhost:3306表示数据库服务器位置和端口号,your_database_name表示要连接的数据库名称,其余参数控制时区和SSL使用;user
:MySQL用户名;password
:MySQL用户密码。
需要注意的是,此处需要使用try-catch语句来捕获可能出现的异常。
进行内容查询
连接MySQL数据库后,我们可以通过Statement或者PreparedStatement对象执行SQL语句进行内容查询。例如:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class QueryMySQL {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC";
String user = "your_username";
String password = "your_password";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
String sql = "SELECT * FROM your_table_name WHERE column1 = ? AND column2 = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, "value1");
ps.setString(2, "value2");
rs = ps.executeQuery();
while(rs.next()) {
int id = rs.getInt("id");
String column1 = rs.getString("column1");
String column2 = rs.getString("column2");
System.out.println("id: " + id + ", column1: " + column1 + ", column2: " + column2);
}
} catch (ClassNotFoundException | SQLException e) {
System.err.println("Cannot connect to database server");
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) { /* ignored */}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) { /* ignored */}
}
if (conn != null) {
try {
conn.close();
System.out.println("Database connection terminated");
} catch (SQLException e) { /* ignored */}
}
}
}
}
其中:
sql
:要执行的SQL语句,其中?表示预编译语句的参数;ps.setString()
:设置预编译语句中的参数;rs.next()
:将光标向前移动一行,如果有下一行则返回true,否则返回false;rs.getInt()
:获取查询结果集中id
列的值;rs.getString()
:获取查询结果集中column1
和column2
列的值。
这里演示的是通过PreparedStatement对象执行SQL语句进行内容查询。使用Statement对象执行SQL语句可参考以下示例:
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StatementMySQL {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC";
String user = "your_username";
String password = "your_password";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
String sql = "SELECT * FROM your_table_name WHERE column1 = 'value1' AND column2 = 'value2'";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
String column1 = rs.getString("column1");
String column2 = rs.getString("column2");
System.out.println("id: " + id + ", column1: " + column1 + ", column2: " + column2);
}
} catch (ClassNotFoundException | SQLException e) {
System.err.println("Cannot connect to database server");
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) { /* ignored */}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) { /* ignored */}
}
if (conn != null) {
try {
conn.close();
System.out.println("Database connection terminated");
} catch (SQLException e) { /* ignored */}
}
}
}
}
其中:
sql
:要执行的SQL语句;stmt
:Statement对象。
总结
本文详细地介绍了Java连接MySQL数据库并进行内容查询的完整攻略和相关的示例代码,希望对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java mysql数据库并进行内容查询实例代码 - Python技术站