Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package inf.smart.database;
- import java.sql.*;
- public class JDBCExample {
- private static final String URL = "jdbc:mysql://localhost:3306/mydbtest";
- private static final String NAME = "root";
- private static final String PASS = "1234";
- private static Connection con;
- private static Statement stmt;
- private static ResultSet res;
- public static void main(String[] args) {
- try {
- con = DriverManager.getConnection(URL, NAME, PASS);
- if (!con.isClosed()) {
- System.out.println("Connection is established!");
- }
- stmt = con.createStatement();
- stmt.executeUpdate("CREATE TABLE Phones (Name CHAR(15), Phone CHAR(15))");
- stmt.executeUpdate("INSERT INTO Phones VALUES ('Mike', '0671234567')");
- stmt.executeUpdate("INSERT INTO Phones VALUES ('James', '0507654321')");
- stmt.executeUpdate("INSERT INTO Phones VALUES ('Merilyn', '0631234567')");
- stmt.executeUpdate("INSERT INTO Phones VALUES ('Jonathan', '0687654321')");
- res = stmt.executeQuery("SELECT * FROM Phones");
- while (res.next()) {
- String name = res.getString(1);
- String phone = res.getString("Phone");
- System.out.println(name + ": " + phone);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try { con.close(); } catch(SQLException se) { /*can't do anything */ }
- try { stmt.close(); } catch(SQLException se) { /*can't do anything */ }
- try { res.close(); } catch(SQLException se) { /*can't do anything */ }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement