Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. package com.study;
  2.  
  3. import java.sql.*;
  4.  
  5. public class App {
  6. public static void main(String[] args) throws SQLException {
  7. Connection connection =
  8. DriverManager.getConnection("jdbc:mysql://localhost:3306/world", "root", "root");
  9.  
  10. String sql = "Select * from city;";
  11. Statement statement = connection.createStatement();
  12. ResultSet resultSet = statement.executeQuery(sql);
  13. while (resultSet.next()) {
  14. int id = resultSet.getInt("ID");
  15. String name = resultSet.getString("Name");
  16. int population = resultSet.getInt("Population");
  17. System.out.println(id + " " + name + " " + population);
  18. }
  19.  
  20. String preparedStatementSql = "insert into city (ID, Name, Population, CountryCode, District ) " +
  21. "values (?,?,?,?,?)";
  22. PreparedStatement preparedStatement = connection.prepareStatement(preparedStatementSql);
  23. preparedStatement.setInt(1, 7777);
  24. preparedStatement.setString(2, "Dnepr");
  25. preparedStatement.setInt(3, 5000000);
  26. preparedStatement.setString(4, "ARG");
  27. preparedStatement.setString(5, "Babushkinskiy");
  28.  
  29. preparedStatement.executeUpdate();
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement