Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.study;
- import java.sql.*;
- public class App {
- public static void main(String[] args) throws SQLException {
- Connection connection =
- DriverManager.getConnection("jdbc:mysql://localhost:3306/world", "root", "root");
- String sql = "Select * from city;";
- Statement statement = connection.createStatement();
- ResultSet resultSet = statement.executeQuery(sql);
- while (resultSet.next()) {
- int id = resultSet.getInt("ID");
- String name = resultSet.getString("Name");
- int population = resultSet.getInt("Population");
- System.out.println(id + " " + name + " " + population);
- }
- String preparedStatementSql = "insert into city (ID, Name, Population, CountryCode, District ) " +
- "values (?,?,?,?,?)";
- PreparedStatement preparedStatement = connection.prepareStatement(preparedStatementSql);
- preparedStatement.setInt(1, 7777);
- preparedStatement.setString(2, "Dnepr");
- preparedStatement.setInt(3, 5000000);
- preparedStatement.setString(4, "ARG");
- preparedStatement.setString(5, "Babushkinskiy");
- preparedStatement.executeUpdate();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement