JDBC编程及优化(不定期更新。。。)

要使用使用JDBC,首先你要引入驱动,我这里使用的是mySQL,所以我引入驱动:mysql-connector-java

mysql-connector-java各版本下载地址,不蟹

首先将jar包引入

然后编码,如下:

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