请写一个java连接mysql的程序代码
时间: 2023-09-12 07:05:23 浏览: 87
### 回答1:
以下是一个连接MySQL数据库的Java程序代码示例:
```
import java.sql.*;
public class MySQLConnect {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/test";
static final String USER = "root";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println(" 实例化Statement对象...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
```
请注意,在上面的代码中,你需要替换MySQL服务器的用户名("root")和密码("password")以及数据库的URL("jdbc:mysql://localhost/test")为你自己的信息。
### 回答2:
以下是一个简单的Java程序代码,用于连接MySQL数据库:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
public static void main(String[] args) {
// 数据库连接信息
String url = "jdbc:mysql://localhost:3306/mydatabase"; // 数据库连接URL
String username = "root"; // 用户名
String password = "password"; // 密码
try {
// 加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 建立数据库连接
Connection connection = DriverManager.getConnection(url, username, password);
// 创建Statement对象
Statement statement = connection.createStatement();
// 执行SQL查询语句
String sql = "SELECT * FROM mytable";
ResultSet resultSet = statement.executeQuery(sql);
// 处理查询结果
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("ID: " + id + ", Name: " + name);
}
// 关闭资源
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
请确保你已经下载并安装了MySQL数据库,并将代码中的数据库连接URL、用户名和密码替换为你的数据库信息。在这个例子中,我们使用了MySQL Connector/J驱动器来连接MySQL数据库。代码首先加载驱动程序,然后建立数据库连接,创建一个Statement对象,执行SQL查询语句,并处理查询结果。最后,记得关闭相关的资源。
### 回答3:
这是一个简单的Java连接MySQL数据库的程序代码示例:
```java
import java.sql.*;
public class MySQLConnectionDemo {
public static void main(String[] args) {
// 声明连接对象和结果集对象
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
// 加载MySQL驱动
Class.forName("com.mysql.jdbc.Driver");
// 建立数据库连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
// 创建SQL语句执行对象
statement = connection.createStatement();
// 执行SQL查询语句
resultSet = statement.executeQuery("SELECT * FROM mytable");
// 处理查询结果
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接和结果集对象
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
在此示例中,我们首先加载MySQL驱动程序,然后使用`DriverManager.getConnection()`方法建立与MySQL数据库的连接。然后,我们创建一个`Statement`对象来执行SQL查询语句,并使用`executeQuery()`方法执行查询并将结果存储在`ResultSet`对象中。我们可以使用`ResultSet.next()`方法遍历结果集,并通过指定的列名获取相应的数据。最后,我们在循环中输出查询结果,并在程序结尾处关闭数据库连接和结果集对象,以释放资源。注意,需要将代码中的"mydatabase"、"username"和"password"替换为实际的数据库名称、用户名和密码。