Advertisement
Guest User

Untitled

a guest
Jun 7th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import java.util.Date;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import java.lang.Math;
  9.  
  10. /*
  11. import android.location.LocationListener;
  12. import android.location.LocationManager;
  13. import android.location.Location;
  14. */
  15. public class InsertDb {
  16.  
  17.     static Connection con;
  18.  
  19.     public static void main(String[] args) {
  20.         InsertDb m = new InsertDb();
  21. //URL к базе состоит из протокола:подпротокола://[хоста]:[порта_СУБД]/[БД] и других_сведений
  22.         String url = "jdbc:postgresql://168.63.70.226/mobil";
  23. //Имя пользователя БД
  24.         String name = "postgres";
  25. //Пароль
  26.         String password = "P@ssword2018";
  27.         /*
  28.         String data = String.format(
  29.                 "Coordinates: lat = %1$.4f, lon = %2$.4f, time = %3$tF %3$tT",
  30.                 location.getLatitude(), location.getLongitude(), new Date(
  31.                         location.getTime()));
  32.         */
  33.         Date day = new Date();
  34.         String data1 = String.format(
  35.                 "Coordinates: lat = %1$.4f, lon = %2$.4f, time = %3$tF %3$tT",
  36.                 Math.random() * 1000, Math.random() * 1000, day);
  37.         int key = (int) (Math.random() * 1000);
  38.         try {
  39. //Загружаем драйвер
  40.             Class.forName("org.postgresql.Driver");
  41. //Создаём соединение
  42.             con = DriverManager.getConnection(url, name, password);
  43.             try {
  44. // Процедура вставки
  45.                 System.out.println("FSf");
  46.                 m.insert(con, key, data1);
  47.             } finally {
  48.                 con.close();
  49.             }
  50. //закрываем соединение
  51.         } catch (Exception ex) {
  52. //выводим наиболее значимые сообщения
  53.             Logger.getLogger(InsertDb.class.getName()).log(Level.SEVERE, null, ex);
  54.         } finally {
  55.             if (con != null) {
  56.                 try {
  57.                     con.close();
  58.                 } catch (SQLException ex) {
  59.                     Logger.getLogger(InsertDb.class.getName()).log(Level.SEVERE, null, ex);
  60.                 }
  61.             }
  62.         }
  63.     }
  64.  
  65.     private void insert(Connection con, int key, String coord) throws SQLException {
  66.         PreparedStatement stmt = con.prepareStatement("insert into t1 (t1) values (?, ?)");//исправить
  67.         stmt.setInt(1, key);
  68.         stmt.setString(3, coord);
  69.         stmt.executeUpdate();
  70.         con.commit();
  71.         stmt.close();
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement