zoukankan      html  css  js  c++  java
  • 数据库java包

    package com.hgkj.model.dao.impl;

    import java.sql.*;

    public class DBManager {
    private static final String driver="com.mysql.jdbc.Driver";
    private static final String url="jdbc:mysql://localhost:3306/idea?characterEncoding=utf-8";
    private static final String userName="root";
    private static final String userPwd="123";

    /**
    * 获取数据库连接
    * @return
    */
    public static Connection getConnection(){
    Connection conn=null;
    try {
    //加载驱动
    Class.forName(driver);
    //获取数据库连接
    conn=DriverManager.getConnection(url, userName, userPwd);
    } catch (Exception e) {
    System.out.println("数据库连接异常");
    e.printStackTrace();
    }
    return conn;
    }
    /**
    * 使用PrepareStatement封装增删改操作
    * @param sql
    * @return
    */
    public static boolean executeUpdate(String sql,Object[] objs){
    Connection conn=null;
    PreparedStatement pst=null;
    boolean flag=false;
    try {
    conn=getConnection();
    pst=conn.prepareStatement(sql);
    //给参数赋值
    for(int i=0;i<objs.length;i++){
    pst.setObject(i+1, objs[i]);
    }
    int num=pst.executeUpdate();
    if(num>0){
    flag=true;
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }finally{
    closeAll(conn, pst);
    }

    return flag;
    }
    /**
    * 关闭连接对象
    * @param conn
    * @param state
    * @param rs
    */
    public static void closeAll(Connection conn,Statement state,ResultSet rs){
    try {
    if(rs!=null){
    rs.close();
    }
    if(state!=null){
    state.close();
    }
    if(conn!=null){
    conn.close();
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    public static void closeAll(Connection conn,Statement state) {
    try {
    if (state != null) {
    state.close();
    }
    if (conn != null) {
    conn.close();
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    }

  • 相关阅读:
    【jQuery】用jQuery给文本框添加只读属性【readOnly】
    解决embed标签显示在div上层【转藏】
    width:100% 和 max-width:100%; 有区别吗【转藏】
    一位资深程序员的独白
    jQuery 取值、赋值的基本方法【转藏】
    js判断手机端操作系统(Andorid/IOS)
    PhpStrom 和 wamp 配置 xdebug
    php 中 ?? 和 empty 的 区别
    phpSpreadSheet 中 使用的 一些坑
    html td 限制 高度 和 宽度
  • 原文地址:https://www.cnblogs.com/wzn520/p/10944521.html
Copyright © 2011-2022 走看看