Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.ResultSetMetaData;
- import com.mysql.jdbc.Statement;
- public class SQLManager {
- private String host;
- private String user;
- private String pass;
- private String dbnm;
- private Connection conn = null;
- private Statement stat = null;
- public SQLManager(String host, String user, String pass, String dbnm) {
- this.host = host;
- this.user = user;
- this.pass = pass;
- this.dbnm = dbnm;
- System.out.println("Connecting to " + host + dbnm + " ...");
- try {
- conn = (Connection) DriverManager.getConnection(host + dbnm, user, pass);
- stat = (Statement) conn.createStatement();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- System.out.println("Connected!");
- }
- }
- public ResultSet select(String query) {
- ResultSet resS = null;
- try {
- resS = stat.executeQuery(query);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return resS;
- }
- public int update(String query) {
- int resU = 0;
- try {
- resU = stat.executeUpdate(query);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return resU;
- }
- public ArrayList<String> getResult(ResultSet res) {
- ArrayList<String> result = new ArrayList<String>();
- try {
- ResultSetMetaData rsmd = (ResultSetMetaData) res.getMetaData();
- int columnsNumber = rsmd.getColumnCount();
- while (res.next()) {
- for (int i = 1; i <= columnsNumber; i++) {
- String columnValue = res.getString(i);
- result.add(columnValue);
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement