Advertisement
keymasterviriya1150

Untitled

Mar 15th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1.  
  2. //dbjava
  3. import java.sql.*;
  4.  
  5. public class MyJDBC {
  6.  
  7.     private static final String MYURL = "jdbc:mysql://localhost/army";
  8.     private static final String USERNAME = "root";
  9.     private static final String PASSWORD = "";
  10.  
  11.     public static void main(String[] args) {
  12.         Connection con = null;
  13.         try {
  14.             Class.forName("com.mysql.jdbc.Driver");
  15.             con = DriverManager.getConnection(MYURL, USERNAME, PASSWORD);
  16.             if (con != null) {
  17.                 System.out.println("Successfully connect");
  18.             } else {
  19.                 System.out.println("Connection fail!");
  20.             }
  21.             Statement stmt = con.createStatement();
  22.            
  23.             String army = "INSERT INTO mytable_5730213093  VALUES (6,'David')";
  24.             stmt.executeUpdate( army);
  25.             army = "INSERT INTO mytable_5730213093  VALUES (7,'Jacob')";
  26.             stmt.executeUpdate( army);
  27.             army = "INSERT INTO mytable_5730213093  VALUES (8,'Nick')";
  28.             stmt.executeUpdate( army);
  29.            army= "INSERT INTO mytable_5730213093  VALUES (9,'Ronald')";
  30.             stmt.executeUpdate( army);
  31.            
  32.             PreparedStatement Statement = con.prepareStatement("UPDATE mytable_5730213093 SET NAME = 'Donna' WHERE ID = 1");
  33.             PreparedStatement Statement2 = con.prepareStatement("UPDATE mytable_5730213093 SET NAME = 'Xio' WHERE ID = 2");
  34.             Statement.executeUpdate();
  35.             Statement2.executeUpdate();
  36.            
  37.            
  38.             ResultSet rs = stmt.executeQuery("SELECT * FROM mytable_5730213093 ");
  39.             while (rs.next()) {
  40.                 int num = rs.getInt("ID");
  41.                 String name = (String) rs.getString("NAME");
  42.                 System.out.println(num + ":" + name);
  43.  
  44.             }
  45.            
  46.         } catch (Exception e) {
  47.             e.printStackTrace();
  48.         } finally {
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement