JDBC programming and optimization (updated from time to time ...)

To use JDBC, first you have to drive the introduction, I used here is mySQL, so I introduced drivers: mysql-connector-java

mysql-connector-java versions Download, no crab

First introduced into the jar package

The encoded as follows:

package com.chudonghai;

/**
 * @author Alpha
 * @GL:Alibaba Java Coding Guidelines
 * */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcTest2 {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 连接数据库
        // 1.将驱动加载到内存中
        Class.forName("com.mysql.jdbc.Driver");
        // 2.写上你要连接的数据库的   地址:端口号/数据库名
        String url = "jdbc:mysql://127.0.0.1:3306/mango";
        // 3.用户名
        String user = "root";
        // 4.密码
        String password = "1234";

        // 建立数据库连接
        Connection conn = DriverManager.getConnection(url, user, password);
        // 写SQL,参数最好采用占位符
        String sql = " select * from sys_config where type= ? ";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, "color");
            // 执行查询
            rs = ps.executeQuery();

            // 定义接收字段信息
            int id;
            String value, label;
            // 表头
            System.out.println("id\t值\t标签\t");
            // 遍历结果集并输出
            while (rs.next()) {
                //根据字段名,获取结果集中对应的字段值
                id = rs.getInt("id");
                value = rs.getString("value");
                label = rs.getString("label");
                System.out.println(id + "\t" + value + "\t" + label);
            }
            // 关闭数据库连接(这个要在结果集处理之后关闭)
            conn.close();
        } catch (Exception e) {
            throw e;
        } finally {
            if (rs != null) {try {rs.close();} catch (SQLException e) {rs = null;}}
            if (ps != null) {try {ps.close();} catch (SQLException e) {ps = null;}}
        }
    }

}

Here in fact can be optimized: the load, connect to the database a series of problems, isolated. Optimized code as follows:

package com.chudonghai;

/**
 * @author Alpha
 * @GL:Alibaba Java Coding Guidelines
 * */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcTest {
    /**
     * 全局变量
     * */
    static Connection conn = null;
    static PreparedStatement ps = null;
    static ResultSet rs = null;

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 连接数据库
        conn();
        // 写SQL,参数最好采用占位符
        String sql = " select * from sys_config where type= ? ";
        try {
            //解析路径
            ps = conn.prepareStatement(sql);
            //替换参数
            ps.setString(1, "color");
            // 执行查询
            rs = ps.executeQuery();
            
            // 定义接收字段信息
            int id;
            String value, label;
            // 表头
            System.out.println("id\t值\t标签\t");
            // 遍历结果集并输出
            while (rs.next()) {
                //根据字段名,获取结果集中对应的字段值
                id = rs.getInt("id");
                value = rs.getString("value");
                label = rs.getString("label");
                System.out.println(id + "\t" + value + "\t" + label);
            }
            // 关闭数据库连接(这个要在结果集处理之后关闭)
            conn.close();
        } catch (Exception e) {
            throw e;
        } finally {
            if (rs != null) {try {rs.close();} catch (SQLException e) {rs = null;}}
            if (ps != null) {try {ps.close();} catch (SQLException e) {ps = null;}}
        }
    }
    
    /**
     * 数据库连接方法
     * */
    public static void conn() {
        // 1.将驱动加载到内存中
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
        // 2.写上你要连接的数据库的   地址:端口号/数据库名
        String url = "jdbc:mysql://127.0.0.1:3306/mango";
        // 3.用户名
        String user = "root";
        // 4.密码
        String password = "1234";
        // 5.建立数据库连接
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

Only.

Guess you like

Origin www.cnblogs.com/chudonghai/p/11326629.html