JDBC 프로그래밍 및 최적화 (수시로 업데이트 ...)

JDBC를 사용하려면 먼저 당신은 내가 여기에 사용 MySQL을하고, 도입을 운전해야, 그래서 드라이버를 소개 : MySQL의 커넥터 - 자바

MySQL의 커넥터 - 자바 버전 다운로드, 어떤 게 없다

먼저 항아리 패키지에 도입

다음과 같이 인코딩 :

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;}}
        }
    }

}

여기 사실을 최적화 할 수 있습니다 : 부하가 데이터베이스에 고립 된 일련의 문제를 연결합니다. 다음과 같이 코드를 최적화 :

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();
        }
    }

}

이 단지.

추천

출처www.cnblogs.com/chudonghai/p/11326629.html