Advertisement
Guest User

Untitled

a guest
Oct 9th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class Main {
  4.  
  5.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  6.     static final String DB_URL = "jdbc:mysql://localhost/gistda_db";
  7.  
  8.     //  Database credentials
  9.     static final String USER = "root";
  10.     static final String PASS = "wiriya11403";
  11.  
  12.  
  13.     public static void main(String[] args){
  14.         Connection conn = null;
  15.         Statement stmt = null;
  16.         try{
  17.             //STEP 2: Register JDBC driver
  18.             Class.forName(JDBC_DRIVER);
  19.  
  20.             //STEP 3: Open a connection
  21.             System.out.println("Connecting to database...");
  22.             conn = DriverManager.getConnection(DB_URL,USER,PASS);
  23.  
  24.             System.out.println("Creating statement...");
  25.             stmt = conn.createStatement();
  26.             String sql = "SELECT id FROM optemis_job ORDER BY id DESC limit 1";
  27.             ResultSet rs = stmt.executeQuery(sql);
  28.             int job_id = 0;
  29.             while(rs.next()){
  30.                 job_id  = rs.getInt("id");
  31.             }
  32.             rs.close();
  33.  
  34.  
  35.             sql = "UPDATE optemis_job SET percent = 10 WHERE id=" + job_id;
  36.             stmt.executeUpdate(sql);
  37.             Thread.sleep(2000);
  38.  
  39.             sql = "UPDATE optemis_job SET percent = 30 WHERE id=" + job_id;
  40.             stmt.executeUpdate(sql);
  41.             Thread.sleep(2000);
  42.             sql = "UPDATE optemis_job SET percent = 50 WHERE id=" + job_id;
  43.             stmt.executeUpdate(sql);
  44.             Thread.sleep(2000);
  45.             sql = "UPDATE optemis_job SET percent = 70 WHERE id=" + job_id;
  46.             stmt.executeUpdate(sql);
  47.             Thread.sleep(2000);
  48.             sql = "UPDATE optemis_job SET percent = 100 WHERE id=" + job_id;
  49.             stmt.executeUpdate(sql);
  50.             stmt.close();
  51.             conn.close();
  52.         }catch(SQLException se){
  53.             //Handle errors for JDBC
  54.             se.printStackTrace();
  55.         }catch(Exception e){
  56.             //Handle errors for Class.forName
  57.             e.printStackTrace();
  58.         }finally{
  59.             //finally block used to close resources
  60.             try{
  61.                 if(stmt!=null)
  62.                     stmt.close();
  63.             }catch(SQLException se2){
  64.             }// nothing we can do
  65.             try{
  66.                 if(conn!=null)
  67.                     conn.close();
  68.             }catch(SQLException se){
  69.                 se.printStackTrace();
  70.             }//end finally try
  71.         }//end try
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement