Guest User

Untitled

a guest
Jul 11th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package pks.database;
  2.  
  3. import java.sql.CallableStatement;
  4. import java.sql.Connection;
  5. import java.sql.Date;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Time;
  11. import java.sql.Timestamp;
  12.  
  13.  
  14. public class DatabaseConnector
  15. {
  16.     private static Connection connection = null;
  17.     public static void connect(String host, String database, String username, String password) throws Exception
  18.     {
  19.         Class.forName("com.mysql.jdbc.Driver");
  20.         connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database + "?" + "user=" + username+ "&password" + password + "&characterEncoding=utf-8");
  21.     }
  22.     public static ResultSet getCourseList(String cityA, String cityB, String date) throws SQLException
  23.     {
  24.         CallableStatement call = connection.prepareCall("{ call KursyMiedzyPrzystankami(?, ?, ?)}");
  25.         call.setString(1, cityA);
  26.         call.setString(2, cityB);
  27.         call.setDate(3, java.sql.Date.valueOf(date));
  28.         System.out.println(call);
  29.         ResultSet resultSet = call.executeQuery();
  30.         return resultSet;
  31.     }
  32.     private static void setStatementArguments(PreparedStatement statement, Object... args) throws SQLException
  33.     {
  34.         int i = 1;
  35.         for (Object arg : args)
  36.         {
  37.             Class<?> c = arg.getClass();
  38.             if (c.equals(String.class))
  39.                 statement.setString(i, (String)arg);
  40.             else if (c.equals(Integer.class))
  41.                 statement.setInt(i, (Integer)arg);
  42.             else if (c.equals(Date.class) || c.equals(java.util.Date.class))
  43.                 statement.setDate(i, (Date)arg);
  44.             else if (c.equals(Time.class))
  45.                 statement.setTime(i, (Time)arg);
  46.             else if (c.equals(Timestamp.class))
  47.                 statement.setTimestamp(i, (Timestamp)arg);
  48.             else if (c.equals(Float.class))
  49.                 statement.setFloat(i, (Float)arg);
  50.             i++;
  51.         }
  52.         System.out.println(statement);
  53.     }
  54.     public static ResultSet executeCall(String query, Object ... args) throws SQLException
  55.     {
  56.         CallableStatement call = connection.prepareCall("{ " + query + " }");
  57.         setStatementArguments(call, args);
  58.         return call.executeQuery();
  59.     }
  60.     public static ResultSet executeQuery(String query, Object... args) throws SQLException
  61.     {
  62.         PreparedStatement statement = connection.prepareStatement(query);
  63.         setStatementArguments(statement, args);
  64.         return statement.executeQuery();
  65.     }
  66.     public static void executeUpdate(String query, Object... args) throws SQLException
  67.     {
  68.         PreparedStatement statement = connection.prepareStatement(query);
  69.         setStatementArguments(statement, args);
  70.         statement.executeUpdate();
  71.     }
  72. }
Add Comment
Please, Sign In to add comment