idea database connection using jdbc

step:

1, idea to create a maven project;

2, create a test database in the mysql;

3, type the following code:

//根据自己定义的包名填写
package com.xxx;

import java.sql.*;

public class JdbcTest {
    //加载驱动
    //创建连接
    //设置sql语句
    //创建statement
    //设置参数
    //执行查询,得到ResultSet
    //遍历ResultSet,输出结果
    //释放资源
    public static void main(String[] args) {
        System.out.println("hello world,jdbc!");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");

            // 通过驱动管理类获取数据库链接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8", "填入用户名", "填入密码");
            // 定义sql语句 ?表示占位符
            String sql = "select * from user where name = ?";
            // 获取预处理statement
            preparedStatement = connection.prepareStatement(sql);
            // 设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值
            preparedStatement.setString(1, "李四");
            // 向数据库发出sql执行查询,查询出结果集
            resultSet = preparedStatement.executeQuery();
            // 遍历查询结果集
            while (resultSet.next()) {
                System.out.println(resultSet.getString("id") + "  " + resultSet.getString("name"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

When testing found the problem:

1, Class.forName ( "com.mysql.jdbc.Driver"); this is where the loaded database-driven? Compile a start, it was found that the driver can not be found, so look at the path of the class driver, later found in pom.xml which is not introduced into the drive, so after adding automatically download, compile.

<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.25</version>
</dependency>

Summary The following steps:

1, using jdbc programming requires connection to the database, the database information and registration drive

   2, the operation Connection, opens Statement object

   3, performed by the SQL Statement object and returns the result to the object ResultSet

   4, the read data using ResultSet, then into concrete objects through code POJO

   5. Close the resource database-related.

Review and consider the following points:

1, if every time the database operations need to re-open the connection, if the data operation is completed, you need to close the database connection? Whether the database connection can be opened when the program is turned on, close the connection when the program closes. Whether this will consume resources? (Frequent operation of the database connection, the database will consume resources)

2, but also to modify the java code when you modify the sql statement, easy to maintain;

to be continued. . . .

Published 125 original articles · won praise 9 · views 30000 +

Guess you like

Origin blog.csdn.net/jiezhang656/article/details/103677916