Yatin99

Untitled

Oct 25th, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 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 urlshortner;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. /**
  17.  *
  18.  * @author Yatin
  19.  */
  20. public class DataManager
  21. {
  22.     private static final String USERNAME="root";
  23.     private static final String PASSWORD="";
  24.     private static final String CONN_STRING="jdbc:mysql://localhost:3306/urldatabase";
  25.    
  26.     private static Connection mConnection;  //to stablish conection with database;
  27.     private static ResultSet mResultSet;
  28.     private static boolean dataBaseStatus;
  29.  
  30.     public DataManager() throws SQLException
  31.     {
  32.          dataBaseStatus= connectDatabase();
  33.     }
  34.    
  35.     static boolean connectDatabase() // connect to sql database
  36.     {
  37.         try
  38.         {  
  39.             mConnection=(Connection) DriverManager.getConnection(CONN_STRING,USERNAME,PASSWORD);
  40.             return  true;
  41.            
  42.         } catch (SQLException ex)
  43.         {
  44.             Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
  45.             return false;
  46.         }    
  47.     }
  48.    
  49.    
  50.     boolean insetURL(int index,String longURL,String smallURL)
  51.     {    
  52.         String sqlQuery="INSERT INTO urlmapping (Index_no,LongURL,SmallURL) VALUES("+index+",'"+longURL+"','"+smallURL+"')";
  53.         try
  54.         {
  55.             PreparedStatement InsertData=mConnection.prepareStatement(sqlQuery);
  56.             InsertData.execute();
  57.             return true;
  58.         } catch (SQLException ex)
  59.         {
  60.             Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
  61.             return false;
  62.         }
  63.     }
  64.    
  65.     String retriveURL(int index)
  66.     {
  67.         String sqlQuery="SELECT LongURL FROM urlmapping WHERE Index_no="+index;
  68.         String longURL = null;
  69.         try
  70.         {
  71.             PreparedStatement RetrveData=mConnection.prepareStatement(sqlQuery);
  72.             mResultSet=RetrveData.executeQuery();
  73.             while(mResultSet.next())
  74.                 longURL=mResultSet.getString("LongURL");
  75.             return longURL;
  76.         } catch (SQLException ex)
  77.         {
  78.             Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
  79.              return null;
  80.         }    
  81.     }
  82.    
  83.     int getEntryCount() throws SQLException  // gets the count of entries for the counter variable
  84.     {
  85.        
  86.         int count=0;
  87.         String sqlQuery=" SELECT COUNT(*) FROM urlmapping";
  88.         try
  89.         {          
  90.             PreparedStatement getCount=mConnection.prepareStatement(sqlQuery);
  91.             mResultSet=getCount.executeQuery();
  92.             while(mResultSet.next())
  93.                 count=Integer.parseInt(mResultSet.getString("count(*)"));
  94.             return count;
  95.            
  96.         }catch(SQLException ex)
  97.         {
  98.             Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
  99.             return -1;
  100.         }
  101.     }
  102.    
  103.    
  104. }
Add Comment
Please, Sign In to add comment