Friday 1 February 2013

Login with Swings


Create a Login form and Index form with netbeans ide and inside Login form you can put two labels Name and password with a jTextField1 and jPasswordField1 and two buttons Submit and Cancel

Login
Submit
  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
        String s=jTextField1.getText();
        String s1=jPasswordField1.getText();
        Index i=new Index();              
        if(s.equals("Vikram")&&s1.equals("vikram"))
        {
            i.setVisible(true);
        }
        else if(!"Vicky".equals(s))
        {
            JOptionPane.showMessageDialog(this,"User Name Is Incorrect");
        }
        else
        {
           
            JOptionPane.showMessageDialog(this,"Password Is Incorrect");
        }
}
Cancel
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) 
{
    System.exit(0);
}
Index
Date can be set to the textfield using the below code :
  
  private void formWindowActivated(java.awt.event.WindowEvent evt) 
{
        Date d=new Date();
        SimpleDateFormat da=new SimpleDateFormat("dd/MM/yyyy");
        jTextField1.setText(da.format(d));
        SimpleDateFormat vi=new SimpleDateFormat("hh:mm:ss");
        jTextField2.setText(vi.format(d));
}


Jsp connection with mysql database


Create a folder named user inside your web server (Tomcat) C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps and inside the user create WEB-INF and META-INF so that you can create a file inside META-INF  named Content.xml  and you need to create a empty folder WEB-INF  and you can create and place the file test.jsp inside C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\user

Content.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/user"/>

test.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>

<html>
<head>
<title>Connection with mysql database</title>
</head>
<body>
<h1>Connection status </h1>

<%
String connectionURL = "jdbc:mysql://localhost:3306/usermaster";
Connection connection = null;
ResultSet rst=null;
Statement st=null;

try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "a");
st = connection.createStatement();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
if(request.getParameter("action") != null)
{
String un=request.getParameter("un");
String pw=request.getParameter("pwd");
rst=st.executeQuery("select * from login");
%>

<h2>Login/h2>
<table border="1" cellspacing="0" cellpadding="0">
<tr>

<td><b>User name</b></td>
<td><b>Password</b></td>
</tr>
<%

while(rst.next()){
%>
<tr>

 <td><%=rst.getString("un")%></td>
 <td> <%=rst.getString("pwd")%></td>
</tr>

<%

rst.close();
st.close();
connection.close();

%>
</table>
</body>
</html>
Please provide feedback readers.
Thank you
Vikram.S