Showing posts with label MYSQL. Show all posts
Showing posts with label MYSQL. Show all posts

Friday, 22 March 2019

How to recover data/databases/tables from mysql in WAMP server.

Suppose you are running WAMP server on your local system and due to bad luck your WAMP server got corrupted or unable to start. You must have created some databases and tables in your MYSQL, then you would be unable to access these data. So, now here question arises that, any how we can get this data/databases/tables and recover our databases and tables.

Answer is "yes".

There might be several ways to recover data, but what I applied was very simple and hassle free. Let see, how I recovered my data.

I have applied this method on WAMP Server 3.1.7 on 64 bit Windows Server 2016.

Copy your old WAMP server data of location your_root_wampserver_folder\bin\mysql\mysql5.7.24\data ( in my case it was D:\wamp64\bin\mysql\mysql5.7.24\data ). Copy either data folder or data of data folder for recovery to any location.

Note:- You can see your all databases name as folder in data folder, you have been previously created.

Now back up your new installed WAMP server MYSQL data folder (location your_root_wampserver_folder\bin\mysql\mysql5.7.24\data) data to any safe location.

 After backing up after data folder of your new system, copy the old data folder to new data folder.

Important :- Both WAMP versions should be same.

Monday, 28 November 2016

How to authenticate a user (form based login example) in Java and create a session.

Everyone is familiar now days from login. Login is required everywhere when you do some work on web related to you or your account. Many web applications register users and manage their accounts for the security purpose.
I don’t go in details about these. I only focus on how do we authenticate user and create a session for that user in Java. I assume that the reader has the basic knowledge of database and java database connectivity as well as starting a new project to develop.
So prerequisite of this example is:
  1. A working sever for handle java code(preferable Apache tomcat)
  2. A database with user table and has some user list with their passwords ( I have used MYSQL database).
  3. Knowledge of HTML to create login form.
Now first you have to create a login page using HTML, so that users can enter their username and password.
html.html
<form id="form1" method="post" action="login.jsp">
Username<input name="username" type="text" size="35" style="height:30px;" placeholder="Enter Your Username" required />
Password<input name="password" type="password" size="35" style="height:30px;" placeholder="Enter Your Password" required />
<input type="reset" name="Reset" value="Reset" />
<input type="submit" name="Submit2" value="Submit" />
</form>

To authenticate credentials entered by a user we require a java code (written in login.jsp file) to authenticate user from user list table of database.
login.jsp

//Get data from login page

String username=req.getParameter("username");
          String password=req.getParameter("password");
//Database connectivity
Connection conn = null;
          Statement stmt=null;
           String user="";
          String pass="";
          Class.forName("com.mysql.jdbc.Driver");
          conn =           DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name","DB_username","DB_password");
          stmt = conn.createStatement();

//Execute SQL query to search in database to authenticate user
String sql="select username, password, type from users where        username='"+username+"' and password='"+password+"'"
ResultSet rs = stmt.executeQuery(sql);

//If user found in database store his details in variables
if(rs.next())
{
   user=rs.getString(1);
pass=rs.getString(2);
type=rs.getString(3);
}
//If user is valid than create a session and redirect him to his account page.
if(username.equals(user) && pass.equals(password) )
{
HttpSession session = reqest.getSession(true);
session.setAttribute("user", username);
response.sendRedirect("userhome.jsp");
}

//If user is not valid then redirect him to login page
 else
{
response.sendRedirect("login.jsp");
}
//Close database connection.
rs.close();
stmt.close();
conn.close();

The complete java code is
try
{
String username=req.getParameter("username");
String password=req.getParameter("password");
Connection conn = null;
 Statement stmt=null;
String user="";
String pass="";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","DB_user","DB_password");
stmt = conn.createStatement();
String sql="select username, password, type from users where username='"+username+"' and password='"+password+"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
{
user=rs.getString(1);
pass=rs.getString(2);
 }
if(username.equals(user) && pass.equals(password) )
{
HttpSession session = request.getSession(true);
session.setAttribute("user", username);
response.sendRedirect("home.jsp");
}
else
{
response.sendRedirect("login.jsp");
}
rs.close();
stmt.close();
conn.close();
 }
catch(Exception e)
{
out.println(e);
 }

Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...