码迷,mamicode.com
首页 > 数据库 > 详细

数据库连接类

时间:2020-01-01 20:51:53      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:use   zone   style   try   time   col   prepare   rman   dup   

package util;

import java.sql.*;

public class DBconn {
    static String url = "jdbc:mysql://localhost:3306/student?serverTimezone=UTC";
    static String username = "root";
    static String password = "123";
    static Connection  conn = null;
    static ResultSet rs = null;
    static PreparedStatement ps =null;
    public static void init(){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            System.out.println("init [SQL驱动程序初始化失败!]");
            e.printStackTrace();
        }

    }


    public static int addUpdDel(String sql){
        int i = 0;
        try {
            ps =  conn.prepareStatement(sql);
            i =  ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("sql数据库增删改异常");
            e.printStackTrace();
        }

        return i;
    }
    public static ResultSet selectSql(String sql){
        try {
            ps =  conn.prepareStatement(sql);
            rs =  ps.executeQuery();
        } catch (SQLException e) {
            System.out.println("sql数据库查询异常");
            e.printStackTrace();
        }
        return rs;
    }
    public static void closeConn(){
        try {
            conn.close();
        } catch (SQLException e) {
            System.out.println("sql数据库关闭异常");
            e.printStackTrace();
        }
    }
}

数据库连接类

标签:use   zone   style   try   time   col   prepare   rman   dup   

原文地址:https://www.cnblogs.com/mac-13/p/12129659.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!