• 记账本程序三


    今天写了在src里面连接数据库的代码

    用的数据库为MySQYL

    DBUtil.java

    package com.hjf.util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    /**
     * 数据库连接工具
     * @author Hu
     *
     */
    public class DBUtil {
        
        public static String db_url = "jdbc:mysql://localhost:3306/shangpin?useUnicode=true&characterEncoding=utf8&useSSL=false";
        public static String db_user = "root";
        public static String db_pass = "123";
        
        public static Connection getConn () {
            Connection conn = null;
            
            try {
                Class.forName("com.mysql.jdbc.Driver");//加载驱动
                conn = DriverManager.getConnection(db_url, db_user, db_pass);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            return conn;
        }
        
        /**
         * 关闭连接
         * @param state
         * @param conn
         */
        public static void close (Statement state, Connection conn) {
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public static void close (ResultSet rs, Statement state, Connection conn) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) throws SQLException {
            Connection conn = getConn();
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            String sql ="select * from course";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if(rs.next()){
                System.out.println("空");
            }else{
                System.out.println("不空");
            }
        }
    }
  • 相关阅读:
    [转]ASP.NET中JSON的序列化和反序列化
    [转]JavaScriptSerializer中日期序列化
    [转]国外英语教学网页
    [转]linq to sql (Group By/Having/Count/Sum/Min/Max/Avg操作符)
    [转]Business Model Canvas(商业模式画布):创业公司做头脑风暴和可行性测试的一大利器
    [转]sql server transaction
    CentOS7安装配置PostgreSQL9.6
    使用struts的同步令牌避免form的重复提交
    Flink初探-为什么选择Flink
    jdbc三种常见用法
  • 原文地址:https://www.cnblogs.com/lishengming00/p/10430616.html
Copyright © 2020-2023  润新知