Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* */ public static String host;
- /* */ public static String port;
- /* */ public static String database;
- /* */ public static String username;
- /* */ public static String password;
- /* */ public static Connection con;
- /* */
- /* */ public static void connect()
- /* */ {
- /* 24 */ if (!isConnected()) {
- /* */ try {
- /* 26 */ con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
- /* 27 */ System.out.println("\033[32m[MySQL] Die Verbindung zu MySQL wurde aufgebaut!\033[37m");
- /* */ } catch (SQLException e) {
- /* 29 */ e.printStackTrace();
- /* */ }
- /* */ }
- /* */ }
- /* */
- /* */ public static void disconnect() {
- /* 35 */ if (isConnected()) {
- /* */ try {
- /* 37 */ con.close();
- /* 38 */ System.out.println("\033[31m[MySQL] Die Verbindung wurde geschlossen!\033[37m");
- /* */ } catch (SQLException e) {
- /* 40 */ e.printStackTrace();
- /* */ }
- /* */ }
- /* */ }
- /* */
- /* */ public static boolean isConnected() {
- /* 46 */ return con != null;
- /* */ }
- /* */
- /* */ public static Connection getConnection() {
- /* 50 */ return con;
- /* */ }
- /* */
- /* */ public static void update(String query) {
- /* 54 */ PreparedStatement ps = null;
- /* */ try {
- /* 56 */ ps = getConnection().prepareStatement(query);
- /* 57 */ ps.executeUpdate(); return;
- /* */ } catch (SQLException e) {
- /* 59 */ e.printStackTrace();
- /* */ } finally {
- /* */ try {
- /* 62 */ ps.close();
- /* */ } catch (SQLException e) {
- /* 64 */ e.printStackTrace();
- /* */ }
- /* */ }
- /* */ }
- /* */
- /* */ public static String getString(String query, String object) {
- /* 70 */ PreparedStatement ps = null;
- /* 71 */ ResultSet rs = null;
- /* */ try {
- /* 73 */ ps = getConnection().prepareStatement(query);
- /* 74 */ rs = ps.executeQuery();
- /* 75 */ if (rs.next()) {
- /* 76 */ return rs.getString(object);
- /* */ }
- /* */
- /* */
- /* */
- /* */
- /* */
- /* */
- /* */
- /* */
- /* */
- /* */
- /* 88 */ return null;
- /* */ }
- /* */ catch (SQLException e)
- /* */ {
- /* 79 */ e.printStackTrace();
- /* */ } finally {
- /* */ try {
- /* 82 */ rs.close();
- /* 83 */ ps.close();
- /* */ } catch (SQLException e) {
- /* 85 */ e.printStackTrace();
- /* */ }
- /* */ }
- /* */ }
- /* */
- /* */ public static int getInt(String query, String object)
- /* */ {
- /* 92 */ PreparedStatement ps = null;
- /* 93 */ ResultSet rs = null;
- /* */ try {
- /* 95 */ ps = getConnection().prepareStatement(query);
- /* 96 */ rs = ps.executeQuery();
- /* 97 */ if (rs.next()) {
- /* 98 */ return rs.getInt(object);
- /* */ }
- /* */
- /* */
- /* */
- /* */
- /* */
- /* */
- /* */
- /* */
- /* */
- /* */
- /* 110 */ return 0;
- /* */ }
- /* */ catch (SQLException e)
- /* */ {
- /* 101 */ e.printStackTrace();
- /* */ } finally {
- /* */ try {
- /* 104 */ rs.close();
- /* 105 */ ps.close();
- /* */ } catch (SQLException e) {
- /* 107 */ e.printStackTrace();
- /* */ }
- /* */ }
- /* */ }
- /* */
- /* */
- /* */ public static ArrayList<String> getStringList(String query, String object)
- /* */ {
- /* 115 */ PreparedStatement ps = null;
- /* 116 */ ResultSet rs = null;
- /* */ try {
- /* 118 */ ArrayList<String> list = new ArrayList();
- /* 119 */ ps = getConnection().prepareStatement(query);
- /* 120 */ rs = ps.executeQuery();
- /* 121 */ while (rs.next()) {
- /* 122 */ list.add(rs.getString(object));
- /* */ }
- /* 124 */ return list;
- /* */ } catch (SQLException e) {
- /* 126 */ e.printStackTrace();
- /* */ } finally {
- /* */ try {
- /* 129 */ rs.close();
- /* 130 */ ps.close();
- /* */ } catch (SQLException e) {
- /* 132 */ e.printStackTrace();
- /* */ }
- /* */ }
- /* 135 */ return null;
- /* */ }
- /* */
- /* */ public static ArrayList<Integer> getIntList(String query, String object) {
- /* 139 */ PreparedStatement ps = null;
- /* 140 */ ResultSet rs = null;
- /* */ try {
- /* 142 */ ArrayList<Integer> list = new ArrayList();
- /* 143 */ ps = getConnection().prepareStatement(query);
- /* 144 */ rs = ps.executeQuery();
- /* 145 */ while (rs.next()) {
- /* 146 */ list.add(Integer.valueOf(rs.getInt(object)));
- /* */ }
- /* 148 */ return list;
- /* */ } catch (SQLException e) {
- /* 150 */ e.printStackTrace();
- /* */ } finally {
- /* */ try {
- /* 153 */ rs.close();
- /* 154 */ ps.close();
- /* */ } catch (SQLException e) {
- /* 156 */ e.printStackTrace();
- /* */ }
- /* */ }
- /* 159 */ return null;
- /* */ }
- /* */
- /* */ public static ResultSet getResult(String query) {
- /* 163 */ PreparedStatement ps = null;
- /* 164 */ ResultSet rs = null;
- /* */ try {
- /* 166 */ ps = getConnection().prepareStatement(query);
- /* 167 */ return ps.executeQuery();
- /* */ }
- /* */ catch (SQLException e) {
- /* 170 */ e.printStackTrace();
- /* */ }
- /* 172 */ return null;
- /* */ }
- /* */
- /* */ public static Connection getCon() {
- /* 176 */ return con;
- /* */ }
- /* */ }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement