Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package urlshortner;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Yatin
- */
- public class DataManager
- {
- private static final String USERNAME="root";
- private static final String PASSWORD="";
- private static final String CONN_STRING="jdbc:mysql://localhost:3306/urldatabase";
- private static Connection mConnection; //to stablish conection with database;
- private static ResultSet mResultSet;
- private static boolean dataBaseStatus;
- public DataManager() throws SQLException
- {
- dataBaseStatus= connectDatabase();
- }
- static boolean connectDatabase() // connect to sql database
- {
- try
- {
- mConnection=(Connection) DriverManager.getConnection(CONN_STRING,USERNAME,PASSWORD);
- return true;
- } catch (SQLException ex)
- {
- Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- }
- }
- boolean insetURL(int index,String longURL,String smallURL)
- {
- String sqlQuery="INSERT INTO urlmapping (Index_no,LongURL,SmallURL) VALUES("+index+",'"+longURL+"','"+smallURL+"')";
- try
- {
- PreparedStatement InsertData=mConnection.prepareStatement(sqlQuery);
- InsertData.execute();
- return true;
- } catch (SQLException ex)
- {
- Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- }
- }
- String retriveURL(int index)
- {
- String sqlQuery="SELECT LongURL FROM urlmapping WHERE Index_no="+index;
- String longURL = null;
- try
- {
- PreparedStatement RetrveData=mConnection.prepareStatement(sqlQuery);
- mResultSet=RetrveData.executeQuery();
- while(mResultSet.next())
- longURL=mResultSet.getString("LongURL");
- return longURL;
- } catch (SQLException ex)
- {
- Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
- return null;
- }
- }
- int getEntryCount() throws SQLException // gets the count of entries for the counter variable
- {
- int count=0;
- String sqlQuery=" SELECT COUNT(*) FROM urlmapping";
- try
- {
- PreparedStatement getCount=mConnection.prepareStatement(sqlQuery);
- mResultSet=getCount.executeQuery();
- while(mResultSet.next())
- count=Integer.parseInt(mResultSet.getString("count(*)"));
- return count;
- }catch(SQLException ex)
- {
- Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
- return -1;
- }
- }
- }
Add Comment
Please, Sign In to add comment