Geekerstar

Java数据库工具类参考代码
ConnectionManager.javapackage com.lucene.datasource; imp...
扫描右侧二维码阅读全文
04
2018/03

Java数据库工具类参考代码

ConnectionManager.java

package com.lucene.datasource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager {
    // 方法1:定义一个打开数据库连接
    public Connection openConnection() {
        final String url = "jdbc:mysql://localhost:3306/test";
        final String account = "root";
        final String password = "123456";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, account, password);
            return conn;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 方法2:关闭一个数据库连接
    public void closeConnection(final Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

ConnectionManager_dbcp.java

package com.lucene.datasource;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class ConnectionManager {
    // 方法1:定义一个打开数据库连接
    public Connection openConnection() {
        try {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myData");
            return ds.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 方法2:关闭一个数据库连接
    public void closeConnection(final Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

SQLManager.java

package com.lucene.datasource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SQLManager {
    // 方法1:专门用于执行查询操作的方法
    public ResultSet execQuery(final Connection connection, final String strSQL, final Object... params) {
        try {
            PreparedStatement pstmt = connection.prepareStatement(strSQL);
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            ResultSet rs = pstmt.executeQuery();
            return rs;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 方法2:用于执行增、删、改操作的方法
    public int execUpdate(final Connection connection, final String strSQL, final Object... params) {
        try {
            PreparedStatement pstmt = connection.prepareStatement(strSQL);
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            int affectedRows = pstmt.executeUpdate();
            return affectedRows;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }
}

TransactionManager.java

package com.lucene.datasource;

import java.sql.Connection;
import java.sql.SQLException;

public class TransactionManager {
    // 方法1:开启一个事务
    public void beginTransacation(final Connection connection) {
        if (connection != null) {
            try {
                connection.setAutoCommit(false);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    // 方法2:执行一个事务
    public void commitTranscation(final Connection connection) {
        if (connection != null) {
            try {
                connection.commit();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    // 方法3:回滚事务
    public void rollbackTransacation(final Connection connection) {
        if (connection != null) {
            try {
                connection.rollback();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
最后修改:2018 年 03 月 04 日 05 : 44 PM
如果觉得我的文章对你有用,请随意赞赏

发表评论