Guest User

Untitled

a guest
Oct 25th, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package database;
  7. import java.sql.*;
  8. import java.util.Scanner;
  9. /**
  10.  *
  11.  * @author 17951A05F3
  12.  */
  13. public class Database {
  14.  
  15.     /**
  16.      * @param args the command line arguments
  17.      */
  18.     public static void main(String[] args) {
  19.         while(true)
  20.         {
  21.             System.out.println("1.retrieve\n2.update\n3.exit\n");
  22.             Scanner sc = new Scanner(System.in);
  23.             int temp = sc.nextInt();
  24.             if(temp==1){
  25.                 Database.retrieve();
  26.             }
  27.             if(temp == 2){
  28.                 Database.update();
  29.             }
  30.             else{
  31.                 break;
  32.             }
  33.         }
  34.     }
  35.         public static void retrieve(){
  36.        
  37.             try(Connection conn = DriverManager.getConnection("Jdbc:mysql//localhost:3306/mysql","root","1234")){
  38.                 Statement st1 = conn.createStatement();
  39.                 ResultSet res = st1.executeQuery("select * form student");
  40.                 while(res.next()){
  41.                     System.out.println("id"+res.getInt("id")+"name"+res.getString("name"));
  42.                 }
  43.             }catch(SQLException ex){
  44.                 ex.printStackTrace();
  45.             }
  46.         }
  47.         public static void update(){
  48.            try(Connection conn = DriverManager.getConnection("Jdbc:mysql//localhost:3306/mysql","root","1234")){
  49.                 Statement s = conn.createStatement();
  50.                 s.executeUpdate("Update student SET id = 3 where name = 'hello3'");
  51.             }catch(SQLException ex){
  52.                 ex.printStackTrace();
  53.             }
  54.            
  55.         }
  56.         // TODO code application logic here
  57.     }
Add Comment
Please, Sign In to add comment