- breezedancer
- 等级: 初级会员
- 性别:
- 文章: 6
- 积分: 30
- 来自: 北京
|
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();
- }
- }
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|