SYSTEM LOGIN "JAVA - MySQL"

Friday, October 23, 2009 Cerita Tentang Teknologi Informasi

In the current era of a data security is needed. The data is not merely a collection of files stored in the computer, but the data is not excessive when is the life cycle of human life. For example in the banking system, savings data is critical to guarded kemanannya.
One way to maintain the security of our data is logged in the system, where if the user would enter the system the user must enter a username and password first, and to better maintain, more powerful data is even better if we Encrypt data before the data stored in the database.
In a simple system which we will make this time, we use SQL with MySQL that will be connected with our java program. Because in Java itself has menyediaka libraries for MySQL. Below is a code to connect Java and MySQL:

import java.sql .*;
ConnectMySQL class
(
public void ConnectToMySql ()
(
try
(
Class.forName ( "com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection ( "jdbc: mysql: / / localhost / dbContact", "root ","");
con.close ();
System.out.println ( "Connection to MySQL Server successfully ..!!!");
System.exit (0);
)

catch (ClassNotFoundException eclass)
(
System.out.println ( "Driver does not exist");
)

catch (SQLException esql)
(
System.out.println (esql.getMessage ());
)
)
)
If we do connect successfully then the program will issue the text "Connection to MySQL server successfully!" Then the program will exit. "Jdbc: mysql: / / localhost / dbContact" code indicates the server is used, in this case we use the WAMP server as the MySQL server, so we use localhost, while dbContact is the name of the database used. Root is the user that we use in MySQL is. While double quotation marks ( "") indicates the password of root user, in this case does not use the root password.
Next we will create a query system for checking the password and username, in this case we assume that the password and username is already in the database,

String query = "Select * from tbluser where (username = '" + findUser + "') AND (password = '" + findPass +"')";
try
(
Class.forName ( "com.mysql.jdbc.Driver"); con = DriverManager.getConnection ( "jdbc: mysql: / / localhost / dbcontact", "root ","");
Statement stat = con.createStatement ();
ResultSet rs = stat.executeQuery (query);

if (rs.next ())
(
JOptionPane.showMessageDialog (null, "LogIn success");
frmLogin.setVisible (false);
)

else
(
JOptionPane.showMessageDialog (null, "User not known, please repeat ...", "Login", JOptionPane.WARNING_MESSAGE);
txtUser.setText ("");
txtPass.setText ("");
txtUser.requestFocus ();
)
stat.close ();
con.close ();
)

catch (ClassNotFoundException eclass)
(
JOptionPane.showMessageDialog (null, "Driver does not exist", "Driver", JOptionPane.WARNING_MESSAGE);
)

catch (SQLException esql)
(
JOptionPane.showMessageDialog (null, "Username or password incorrect", "Login", JOptionPane.WARNING_MESSAGE);
txtUser.setText ("");
txtPass.setText ("");
txtUser.requestFocus ();
)

By:
Muhammad Zidny Naf'an
4th semester students Syarif Hidayatullah Jakarta majoring in Computer Science
e-mail: zidny_nafan@yahoo.com

0 comments: SYSTEM LOGIN "JAVA - MySQL"

Archives