⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbutil.java

📁 基于mysql5.0完成的shopping网站后台
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.shopping.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 Administrator */public class DBUtil {    static {        try {            String name = PropertyMgr.getProps("dbDriver");            Class.forName(name);        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }    public static ResultSet query(Statement stmt, String sql) {        ResultSet rs = null;        try {            rs = stmt.executeQuery(sql);        } catch (SQLException e) {            e.printStackTrace();        }        return rs;    }    public static int update(Statement stmt, String sql) {        int rs = 0;        try {            rs = stmt.executeUpdate(sql);        } catch (SQLException e) {            e.printStackTrace();        }        return rs;    }    //获取一个连接    public static Connection getConnection() {        Connection conn = null;        try {            String name1 = PropertyMgr.getProps("dbName1");            String pwd = PropertyMgr.getProps("dbPwd");            String url = PropertyMgr.getProps("dbUrl");            conn = DriverManager.getConnection(url,name1,pwd);        } catch (SQLException e) {            e.printStackTrace();        }        return conn;    }    //    public static Statement getStatement(Connection conn) {        Statement stmt = null;        try {            stmt = conn.createStatement();        } catch (SQLException e) {            e.printStackTrace();        }        return stmt;    }    //    public static PreparedStatement getPStatement(Connection conn, String sql) {        PreparedStatement pstmt = null;        try {            pstmt = conn.prepareStatement(sql);        } catch (SQLException e) {            e.printStackTrace();        }        return pstmt;    }    public static void close(Connection conn, Statement stmt, ResultSet rs) {        try {            if (rs != null) {                rs.close();                rs = null;            }            if (stmt != null) {                stmt.close();                stmt = null;            }            if (conn != null) {                conn.close();                conn = null;            }        } catch (SQLException e) {            e.printStackTrace();        }    }    public static void main(String[] args) {        Connection conn = null;        Statement stmt = null;        try {            conn = getConnection();            stmt = getStatement(conn);            String sql = "insert into user values(null,'tom','tom',111,'china',now())";            System.out.println(sql);            update(stmt, sql);        } catch (Exception e) {            e.printStackTrace();        } finally {            close(conn, stmt, null);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -