rafid_shad

Database

Feb 24th, 2019
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.*;
  4. public class Database {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         String url = "jdbc:mysql://localhost/info";
  9.         String user = "root";
  10.         String password = "root";
  11.         try {
  12.             Connection con = DriverManager.getConnection(url, user, password);
  13.             Statement state = con.createStatement();
  14.            
  15.             String sql="insert into student(id,name,semester)values('171','david','6th')";
  16.             state.executeUpdate(sql);
  17.             String sql2="delete from student where id ='152'";
  18.             state.executeUpdate(sql2);
  19.            
  20.             ResultSet result = state.executeQuery("Select * from student");
  21.             while (result.next()) {
  22.                 System.out.println("Id : " + result.getString("id"));
  23.                 System.out.println("Name: " + result.getString("name"));
  24.                 System.out.println("Semester: " + result.getString("semester"));
  25.                 System.out.println("");
  26.             }
  27.         } catch (Exception ex) {
  28.             ex.printStackTrace();
  29.             System.out.println("Cause : " + ex.getCause());
  30.         }
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment