searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

java连接Mysql

2023-07-05 15:57:32
11
0

加载数据库驱动程序
Class.forName("com.mysql.jdbc.Driver");
创建数据库连接
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "password";
Connection conn = DriverManager.getConnection(url, user, password);
这里的 url 格式为 jdbc:mysql://host:port/database,其中 host 和 port 分别是 MySQL 服务器的主机名和端口号,database 表示连接的数据库名称。user 和 password 是登录 MySQL 数据库所需要的用户名和密码。

创建 Statement 对象,执行 SQL 语句
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM table_name");
while (rs.next()) {
// 处理查询结果
}
在这里,创建了一个 Statement 对象 stmt 用于执行 SQL 查询语句,并通过 executeQuery() 方法执行查询,将查询结果保存在 ResultSet 对象 rs 中,然后使用 while 循环遍历结果集进行处理。

关闭数据库连接
rs.close();
stmt.close();
conn.close();
使用完数据库连接之后应该及时关闭,释放资源。

完整代码:

public class MySQLConnectionDemo {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            // 加载MySQL驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 创建数据库连接
            String url = "jdbc:mysql://localhost:3306/mydatabase";
            String username = "root";
            String password = "password";
            connection = DriverManager.getConnection(url, username, password);

            // 创建Statement对象
            statement = connection.createStatement();

            // 执行查询语句
            String sql = "SELECT * FROM mytable";
            resultSet = statement.executeQuery(sql);

            // 处理查询结果
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                float price = resultSet.getFloat("price");

                System.out.println("ID: " + id + ", Name: " + name + ", Price: " + price);
            }
        } 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();
            }
        }
    }
}

0条评论
作者已关闭评论
t****m
98文章数
1粉丝数
t****m
98 文章 | 1 粉丝
原创

java连接Mysql

2023-07-05 15:57:32
11
0

加载数据库驱动程序
Class.forName("com.mysql.jdbc.Driver");
创建数据库连接
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "password";
Connection conn = DriverManager.getConnection(url, user, password);
这里的 url 格式为 jdbc:mysql://host:port/database,其中 host 和 port 分别是 MySQL 服务器的主机名和端口号,database 表示连接的数据库名称。user 和 password 是登录 MySQL 数据库所需要的用户名和密码。

创建 Statement 对象,执行 SQL 语句
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM table_name");
while (rs.next()) {
// 处理查询结果
}
在这里,创建了一个 Statement 对象 stmt 用于执行 SQL 查询语句,并通过 executeQuery() 方法执行查询,将查询结果保存在 ResultSet 对象 rs 中,然后使用 while 循环遍历结果集进行处理。

关闭数据库连接
rs.close();
stmt.close();
conn.close();
使用完数据库连接之后应该及时关闭,释放资源。

完整代码:

public class MySQLConnectionDemo {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            // 加载MySQL驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 创建数据库连接
            String url = "jdbc:mysql://localhost:3306/mydatabase";
            String username = "root";
            String password = "password";
            connection = DriverManager.getConnection(url, username, password);

            // 创建Statement对象
            statement = connection.createStatement();

            // 执行查询语句
            String sql = "SELECT * FROM mytable";
            resultSet = statement.executeQuery(sql);

            // 处理查询结果
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                float price = resultSet.getFloat("price");

                System.out.println("ID: " + id + ", Name: " + name + ", Price: " + price);
            }
        } 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();
            }
        }
    }
}

文章来自个人专栏
文章 | 订阅
0条评论
作者已关闭评论
作者已关闭评论
0
0