Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package finalProject;
- import java.net.URLDecoder;
- import java.sql.*;
- public class CheckDatabaseConnection {
- private static final String DATABASE = "silvestri";
- private static final String USERNAME = "readonly";
- private static final String PASSWORD = "readonly";
- static String driver = "com.mysql.jdbc.Driver";
- static String url = "jdbc:mysql://cs.stcc.edu/" + DATABASE + "?user=" + USERNAME + "&password=" + PASSWORD;
- static String queryformat = "SELECT C.name, CITY.NAME, CITY.lat, CITY.lng FROM ip4_%d I, countries C, cityByCountry CITY WHERE I.country = C.ID AND I.city = CITY.city AND b = %d AND c = %d;";
- private Connection conn;
- //Connects to server
- public void Connect() {
- try {
- Class.forName(driver).newInstance();
- this.conn = DriverManager.getConnection(url);
- } catch (Exception ex) {
- }
- }
- //CLoses Connection
- public void CloseConnection() throws SQLException {
- this.conn.close();
- }
- //Gets result
- public Result GetResult(int a, int b, int c) throws SQLException {
- String query = String.format(queryformat, a, b, c);
- Statement stat = this.conn.createStatement();
- ResultSet rs = stat.executeQuery(query);
- Result r = new Result();
- rs.next();
- r.Country = URLDecoder.decode(rs.getString(1));
- r.City = URLDecoder.decode(rs.getString(2));
- r.Latitude = rs.getFloat(3);
- r.Longitude = rs.getFloat(4);
- return r;
- }
- //Outputs Result
- class Result {
- public String City;
- public String Country;
- public double Latitude;
- public double Longitude;
- public String toString() {
- return String.format("City: %s;\nCountry: %s;\nLat: %f;\nLong: %f", this.City, this.Country, this.Latitude,
- this.Longitude);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement