MySQL连接数据库查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013160017/article/details/52678866
package com.goldmsg.filepusher.jetty;
/**
 * 连接数据库的工具类,被定义成不可继承且是私有访问
 * Created by zhouhaiming on 2016/9/21.
 */
import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public final class DBUtils {
    private static String url = "jdbc:mysql://loacalhost:3306/apple";
    private static String user = "root";
    private static String psw = "1111111";

    private static  Connection conn;

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    private DBUtils() {

    }

    /**
     * 获取数据库的连接
     * @return conn
     */
    public static Connection getConnection() {
        if(null == conn) {
            try {
                conn = DriverManager.getConnection(url, user, psw);
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return conn;
    }

    /**
     * 释放资源
     * @param conn
     * @param pstmt
     * @param rs
     */
    public static void closeResources(Connection conn,PreparedStatement pstmt,ResultSet rs) {
        if(null != rs) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } finally {
                if(null != pstmt) {
                    try {
                        pstmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    } finally {
                        if(null != conn) {
                            try {
                                conn.close();
                            } catch (SQLException e) {
                                e.printStackTrace();
                                throw new RuntimeException(e);
                            }
                        }
                    }
                }
            }
        }
    }

    public static class MySQLDemo {

        // JDBC 驱动名及数据库 URL
        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        static final String DB_URL = "jdbc:mysql://localhost:3306/store";

        // 数据库的用户名与密码,需要根据自己的设置
        static final String USER = "root";
        static final String PASS = "123456";

        public static void main(String[] args) {
            com.mysql.jdbc.Connection conn = null;
            Statement stmt = null;
            try{
                // 注册 JDBC 驱动
                Class.forName("com.mysql.jdbc.Driver");
                Class.forName(JDBC_DRIVER);

                // 打开链接
                System.out.println("连接数据库...");
                conn = (com.mysql.jdbc.Connection) DriverManager.getConnection(DB_URL,USER,PASS);

                // 执行查询
                System.out.println(" 实例化Statement对...");
    //            stmt = (Statement) conn.createStatement();
                 stmt = (Statement) conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
                String sql;
                sql = "SELECT * FROM obj_replica_task WHERE task_status =0";
                ResultSet rs = stmt.executeQuery(sql);
                // 展开结果集数据库
                while(rs.next()){
                    // 通过字段检索
                    String task_id=rs.getString("task_id");
                    String storage_id=rs.getString("storage_id");
                    // 输出数据
                    System.out.println(task_id);
                    System.out.println(storage_id);
                }
                // 完成后关闭
                rs.close();
                stmt.close();
                conn.close();
            }catch(SQLException se){
                // 处理 JDBC 错误
                se.printStackTrace();
            }catch(Exception e){
                // 处理 Class.forName 错误
                e.printStackTrace();
            }finally{
                // 关闭资源
                try{
                    if(stmt!=null) stmt.close();
                }catch(SQLException se2){
                }// 什么都不做
                try{
                    if(conn!=null) conn.close();
                }catch(SQLException se){
                    se.printStackTrace();
                }
            }
            System.out.println("Goodbye!");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/u013160017/article/details/52678866