Showing posts with label JDBC. Show all posts
Showing posts with label JDBC. Show all posts

Wednesday, September 10, 2014

Insert dropdown value into database. Using JSP SERVLET




Create Dynamic project: File > New > Project....> Web > Dynamic Web Project.


























Create Servlet class with name SampleTest.java(it will automatically makes entry in web.xml)
Paste the following code into doPost()
System.out.println("Selected value is : "+(String)request.getParameter("gameName"));
            String gameName = (String)request.getParameter("gameName");
            DBConnection dbConnection = new DBConnection();
            dbConnection.insertQuery(gameName);
            System.out.println("Query insert successfully");


Create DBConnection.java file


     
     


      static Connection l_objConnection;


      public static Connection getConnetion() {


            try {


                  Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
                  // Your url -jdbc:oracle:thin:@localhost:1521:DBName
                  // your username - Test
                  // your password - Test
                  l_objConnection = DriverManager.getConnection(
                              "jdbc:oracle:thin:@localhost:1521:DBName", "username",
                              "password");


            } catch (InstantiationException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (IllegalAccessException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (ClassNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (SQLException e) {
                  // TODO Auto-generated catch block test
                  e.printStackTrace();
            }


            return l_objConnection;
      }


      public void insertQuery(String gameName) {
            try {
                  // Replace your table name instead of TABLENAME
                  // Replace GAMENAME into colounm name.
                  PreparedStatement preStatement = l_objConnection
                              .prepareStatement("INSERT INTO TABLENAME(GAMENAME) VALUES (?)");
                  preStatement.setString(1, gameName);
                  preStatement.execute();
            } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
      }





Create index.jsp under WebContent folder.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<SCRIPT LANGUAGE="JavaScript">
            <!--
            function button1()
            {
              var value =   document.getElementById("gameName").value;
              alert(value);
              document.SampleTest.submit()
            }   
            
            // -->
        </SCRIPT>
<body>
<FORM NAME="SampleTest" METHOD="POST" action="./SampleTest">
<select id="gameName" name="gameName">
<option>Sports</option>
     <option>Corporate</option>
     <option>Religious</option>
     <option>Music</option>
</select>
<INPUT TYPE="submit" VALUE="Save" ONCLICK="button1()">
</FORM>
</body>
</html>


Web.xml
<servlet>
    <description></description>
    <display-name>SampleTest</display-name>
    <servlet-name>SampleTest</servlet-name>
    <servlet-class>com.test.SampleTest</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>SampleTest</servlet-name>
    <url-pattern>/SampleTest</url-pattern>
  </servlet-mapping>


Output:


 

Thursday, December 6, 2012

JDBC connection for verifying user name and password

/**
 *
 */
package doa;

import java.sql.*;

import dao.LoginBean;

/**
 * @author Boomiraj
 *
 */
public class UserDOA {

    /**
     *
     */
    public UserDOA() {
        // TODO Auto-generated constructor stub
    }
    static Connection con ;
    static ResultSet rs ;
    static Statement stmt ;
    public static LoginBean login(LoginBean lb) {
        //LoginBean lbean = new LoginBean();
        //System.out.println("Test");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","admin");
                stmt = con.createStatement();
                //lb.setUsername("Boomiraj");
                //lb.setPassword("boomi@123");
                String uname = lb.getUsername();
                String pword = lb.getPassword();
                System.out.println(uname);
                System.out.println(pword);
                rs = stmt.executeQuery("select * from login where UserName= '"+uname+"' and PASSWORD='"+pword+"' ");
                boolean more = rs.next();
                if(!more){
                    System.out.println(" Not Matched ");
                }
                if(more){
                    String fname = rs.getString("FirstName");
                    lb.setFirstname(fname);
                    String last = rs.getString("LastName");
                    lb.setLastname(last);
                    lb.setValid(true);
                         }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
       
        
        return lb;
    }
   
   
}