java SQL连接执行类

来源:互联网 发布:顶新影音 mac 编辑:程序博客网 时间:2024/05/20 23:35
package com.zxh.servlet; import java.sql.DriverManager; import java.sql.SQLException; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import com.mysql.jdbc.ResultSet; import com.mysql.jdbc.Statement; public class SQLBean { String dbDriver; String connectionString; String userName; String password; Connection connection = null; Statement statement = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; public SQLBean() { dbDriver = "com.mysql.jdbc.Driver"; connectionString = "jdbc:mysql://localhost/said"; userName = "root"; password = "123456"; } public Connection getConnection() { try { Class.forName(dbDriver); System.out.println("Success loading MySQl driver"); } catch (ClassNotFoundException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } try { connection = (Connection) DriverManager.getConnection( connectionString, userName, password); System.out.println("Success connection MySQL Database"); } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } return connection; } public void executeInsert(String sqlString) { connection = this.getConnection(); try { statement = (Statement) connection.createStatement(); statement.executeUpdate(sqlString); } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } public void executeUpdate(String sqlString) { connection = this.getConnection(); try { statement = (Statement) connection.createStatement(); statement.executeUpdate(sqlString); } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } // String sql = "select * from student"; } public ResultSet executeQuery(String sqlString) { connection = this.getConnection(); try { statement = (Statement) connection.createStatement(); resultSet = (ResultSet) statement.executeQuery(sqlString); } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } return resultSet; } public void executeDelete(String sqlString) { connection = this.getConnection(); try { statement = (Statement) connection.createStatement(); resultSet = (ResultSet) statement.executeQuery(sqlString); } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } public PreparedStatement getPreparedStatement(String sqlString) { connection = this.getConnection(); try { preparedStatement = (PreparedStatement) connection .prepareStatement(sqlString); } catch (SQLException e) { // TODO 自动生成 catch 块 System.out.println("sqlBean connection error"); } return preparedStatement; } public void close() { try { if (statement != null) statement.close(); } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } try { if (connection != null) connection.close(); } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } public String getDbDriver() { return dbDriver; } public void setDbDriver(String dbDriver) { this.dbDriver = dbDriver; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getConnectionString() { return connectionString; } public void setConnectionString(String connectionString) { this.connectionString = connectionString; } }

原创粉丝点击