java 代码
- package com.com.util;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class DBConn {
- private Connection conn = null;
- Statement statement = null;
- ResultSet rs = null;
- public DBConn() {
- try {
- Class.forName("com.mysql.jdbc.Driver");// 加载驱动程序
- conn = DriverManager.getConnection(
- "jdbc:mysql://localhost/actions", "admin", "111111");
- statement = conn.createStatement();
- } catch (Exception e) {
- e.printStackTrace();
- conn = null;
- }
- }
- public DBConn(String dbname) {
- try {
- Class.forName("com.mysql.jdbc.Driver");// 加载驱动程序
- String url = "jdbc:mysql://localhost/" + dbname;
- conn = DriverManager.getConnection(url, "admin", "111111");
- statement = conn.createStatement();
- } catch (Exception e) {
- e.printStackTrace();
- conn = null;
- }
- }
- public void update(String sql) {
- try {
- statement.executeUpdate(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public ResultSet query(String sql) {
- ResultSet rs = null;
- try {
- rs = statement.executeQuery(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return rs;
- }
- public void close() {
- try {
- if (rs != null) {
- rs.close();
- rs = null;
- }
- if (statement != null) {
- statement.close();
- statement = null;
- }
- if (conn != null) {
- conn.close();
- conn = null;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }