Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.Date;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import java.lang.Math;
- /*
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.location.Location;
- */
- public class InsertDb {
- static Connection con;
- public static void main(String[] args) {
- InsertDb m = new InsertDb();
- //URL к базе состоит из протокола:подпротокола://[хоста]:[порта_СУБД]/[БД] и других_сведений
- String url = "jdbc:postgresql://168.63.70.226/mobil";
- //Имя пользователя БД
- String name = "postgres";
- //Пароль
- String password = "P@ssword2018";
- /*
- String data = String.format(
- "Coordinates: lat = %1$.4f, lon = %2$.4f, time = %3$tF %3$tT",
- location.getLatitude(), location.getLongitude(), new Date(
- location.getTime()));
- */
- Date day = new Date();
- String data1 = String.format(
- "Coordinates: lat = %1$.4f, lon = %2$.4f, time = %3$tF %3$tT",
- Math.random() * 1000, Math.random() * 1000, day);
- int key = (int) (Math.random() * 1000);
- try {
- //Загружаем драйвер
- Class.forName("org.postgresql.Driver");
- //Создаём соединение
- con = DriverManager.getConnection(url, name, password);
- try {
- // Процедура вставки
- System.out.println("FSf");
- m.insert(con, key, data1);
- } finally {
- con.close();
- }
- //закрываем соединение
- } catch (Exception ex) {
- //выводим наиболее значимые сообщения
- Logger.getLogger(InsertDb.class.getName()).log(Level.SEVERE, null, ex);
- } finally {
- if (con != null) {
- try {
- con.close();
- } catch (SQLException ex) {
- Logger.getLogger(InsertDb.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- }
- private void insert(Connection con, int key, String coord) throws SQLException {
- PreparedStatement stmt = con.prepareStatement("insert into t1 (t1) values (?, ?)");//исправить
- stmt.setInt(1, key);
- stmt.setString(3, coord);
- stmt.executeUpdate();
- con.commit();
- stmt.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement