package jp.ac.kcska.questionsystem; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class ExcuteDatabase implements AbstractDatabase { @Override public Connection getConnection() { Connection connection = null; try { connection = DriverManager.getConnection( "jdbc:mysql://localhost/questionsystem","root","mysql"); connection.setAutoCommit(false); } catch (SQLException e) { e.printStackTrace(); } return connection; } @Override public ResultSet excuteSelect(String sql) throws SQLException{ Connection connection = getConnection(); PreparedStatement preparedStatement = null; ResultSet result = null; try { preparedStatement = connection.prepareStatement(sql); result = preparedStatement.executeQuery(); } catch (SQLException e) { connection.rollback(); e.printStackTrace(); } return result; } public int excuteUpdate(String sql) throws SQLException{ Connection connection = getConnection(); PreparedStatement preparedStatement = null; int resultCount = -1; try { preparedStatement = connection.prepareStatement(sql); resultCount = preparedStatement.executeUpdate(); connection.commit(); } catch (SQLException e) { e.printStackTrace(); connection.rollback(); } finally{ connection.close(); } return resultCount; } public boolean excuteInsert(String sql) throws SQLException{ Connection connection = getConnection(); boolean flag = false; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); flag = preparedStatement.execute(); connection.commit(); return true; } catch (SQLException e) { connection.rollback(); e.printStackTrace(); }finally{ connection.close(); } return flag; } }