Guest User

Untitled

a guest
Jan 13th, 2019
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. package javatest;
  2.  
  3. import java.sql.*;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class JavaTest {
  8.  
  9.     public static void main(String[] args) {
  10.        
  11.         //Connecting the database
  12.         try{
  13.         Class.forName("com.mysql.jdbc.Driver");
  14.         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employeedetails","root","");
  15.         Statement stmt=conn.createStatement();
  16.        
  17.         //Insert a new record
  18.         String s="INSERT INTO emp VALUES (13,'gfgf','Marketing', 125000)";
  19.         stmt.executeUpdate(s);
  20.        
  21.         //Display all records
  22.         ResultSet res=stmt.executeQuery("Select * from emp");
  23.         while(res.next()){
  24.             System.out.print(res.getInt(1)+" | ");
  25.             System.out.print(res.getString(2)+" | ");
  26.             System.out.print(res.getString(3)+" | ");
  27.             System.out.println(res.getInt(4));
  28.         }
  29.         //Update a record
  30.         String s1="UPDATE emp SET name='Emily' WHERE id=5" ;
  31.         stmt.executeUpdate(s1);
  32.        
  33.         //Delete a record
  34.         String s2="DELETE  FROM emp WHERE name='Bobby'" ;
  35.         stmt.executeUpdate(s2);
  36.         }
  37.        
  38.         catch(SQLException | ClassNotFoundException ex){
  39.             System.out.println(ex.getMessage());
  40.         }
  41.     }
  42. }
Add Comment
Please, Sign In to add comment