Advertisement
Guest User

Untitled

a guest
May 21st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.*;
  3.  
  4. public class exercise_2_kyoung27
  5. {
  6.     public static void main(String [] argv) throws Exception
  7.     {
  8.         //scanner object for keyboard input, database/username/password
  9.         Scanner keyboard = new Scanner(System.in);
  10.         System.out.print("database: ");
  11.         String database = keyboard.nextLine();
  12.        
  13.         System.out.print("user: ");
  14.         String user = keyboard.nextLine();
  15.        
  16.         System.out.print("password: ");
  17.         String password = keyboard.nextLine();
  18.        
  19.        
  20.         Connection conn = null;
  21.         try
  22.         {
  23.           Class.forName("org.postgresql.Driver");
  24.           String url = "jdbc:postgresql://localhost/"+database;
  25.           conn = DriverManager.getConnection(url, user, password);
  26.  
  27.         }
  28.         catch (ClassNotFoundException e)
  29.         {
  30.           e.printStackTrace();
  31.           System.exit(1);
  32.         }
  33.         catch (SQLException e)
  34.         {
  35.           e.printStackTrace();
  36.           System.exit(2);
  37.         }
  38.         //Now we're connected up lets retrieve a list of employees
  39.  
  40.         Statement stmt = null;
  41.         try
  42.         {
  43.             //statement object for inserting
  44.             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
  45.            
  46.             //returns all records from table?
  47.             ResultSet uprs = stmt.executeQuery("SELECT * FROM Movie");
  48.            
  49.             //create new row in ResultSet object
  50.             uprs.moveToInsertRow();
  51.            
  52.             //add new movie data
  53.             uprs.updateInt("movie_id", 76568769);
  54.            
  55.             //insert the new row
  56.             uprs.insertRow();
  57.            
  58.             uprs.beforeFirst();
  59.         }
  60.        
  61.         catch (SQLException e)
  62.         {
  63.             System.out.println(e);
  64.             conn.close();
  65.             System.exit(1);
  66.         }
  67.         System.out.println("yay this worked!");
  68.         conn.close();
  69.       }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement