Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package com.aviadhemo;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.Statement;
  7.  
  8. public class Main {
  9.  
  10.     public static String driver = "org.gjt.mm.mysql.Driver";
  11.     public static String protocol = "jdbc:mysql://localhost:3306/gagamo";
  12.     public static String user = "aviadh";
  13.     public static String password = "200135594";
  14.     public static void main(String[] args)
  15.     {
  16.         Connection connection = null;
  17.         Statement statement = null;
  18.         ResultSet resultSet = null;
  19.  
  20.         try
  21.         {
  22.             connection = null;
  23.             Class.forName(driver);
  24.             connection = DriverManager.getConnection(protocol, user, password);
  25.             statement = connection.createStatement();
  26.             statement.execute("create table inventory(id int, fee double)");
  27.             statement.execute("insert into inventory values (100212,2.5)");
  28.             statement.execute("insert into inventory values (100213,1.2)");
  29.             statement.execute("insert into inventory values (100214,4.2)");
  30.             resultSet = statement.executeQuery(
  31.                     "SELECT id, fee FROM inventory ORDER BY id");
  32.  
  33.             while(resultSet.next()){
  34.                 System.out.println("id=" + resultSet.getInt("id") + " fee=" + resultSet.getDouble("fee"));
  35.             }
  36.  
  37.             statement.execute("DROP TABLE inventory");
  38.         }
  39.         catch(Exception e){
  40.             e.printStackTrace();
  41.         }
  42.         finally{
  43.             if(statement!=null) try{statement.close();}catch(Exception e){}
  44.             if(connection!=null) try{connection.close();}catch(Exception e){}
  45.             if(resultSet != null) try{resultSet.close();}catch(Exception e){}
  46.         }
  47.     }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement