View Single Post
  #1 (permalink)  
Old April 19th, 2006, 07:06 AM
paulhamo paulhamo is offline
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Im trying to insert data into mysql using jsp

Can you advice me on my code, im new to this- im trying to insert data into mysql using a jsp form.. Am i doing this correctly. any help, much appreciated.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>REGISTRATION</title>
</head>

<html>
    <body>
        <form action="Registration.jsp" method=post>

        <center>
        <table cellpadding=4 cellspacing=2 border=0>
        <th bgcolor="#CCCCFF" colspan=2>
        USER REGISTRATION
        <br></th>
        <tr bgcolor="#c8d8f8">
        <td valign=top>

        <b>First Name</b><br>
        <input type="text" name="firstName" value="" size=15 maxlength=20></td>
        <td valign=top>

        <b>Last Name</b><br>
        <input type="text" name="lastName" value="" size=15 maxlength=20></td>
        </tr>
        <tr bgcolor="#c8d8f8">
        <td valign=top>

        <b>E-Mail</b><br>
        <input type="text" name="email" value="" size=25 maxlength=125>
        <br></td>
        <td valign=top>

        <b>Date of Birth </b><br>
        <input type="text" name="dateOfBirth" value="" size=15 maxlength=5></td>
        </tr>
        <tr bgcolor="#c8d8f8">
        <td valign=top colspan=2>
        <div align="center">

        <b>Desired User Name</b><br>
              <input type="text" name="userName" size=30 value="" maxlength=10>
          </div></td>
        </tr>
        <tr bgcolor="#c8d8f8">
        <td valign=top>

        <b>Password</b><br>
        <input type="password" name="password" size=10 value=""
        maxlength=10></td>
        <td valign=top>

        <b>Confirm Password</b><br>
        <input type="password" name="password2" size=10 value=""
        maxlength=10></td>
        <br>
        </tr>
        <tr bgcolor="#c8d8f8">
        <td valign=top colspan=2>
        <div align="center">

        <b>Gender</b>
        <br>
          <input type="radio" name="notify" value="Yes" checked>
        Male
        <input type="radio" name="notify" value="No" >
        Female
        <br>
        <br>
        </div></td>
        </tr>
        <tr bgcolor="#c8d8f8">
        <td align=center colspan=2>

        <input type="submit" value="Submit"> <input type="reset"
        value="Reset">
        </td>
        </tr>

        </table>
        </center>
    </form>

////////////////////////////////jsp code
<%
        Connection conn = null;
    String username= "lovers";
        String password= "project";
    String url= "jdbc:mysql://csserver.ucd.ie/lovers";


    String userName1 = request.getParameter("userName");
        String password1 = request.getParameter("password");
    String eaddress = request.getParameter("email");
        String dob = request.getParameter("dateOfBirth");
        String ************ = request.getParameter("notify");


        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);

            java.sql.Date age= java.sql.Date.valueOf(dob);

            PreparedStatment st;
            st = conn.prepareStatement("INSERT INTO REGISTRATION (USERID, USERNAME, PASSWORD, EMAIL, DATEOFBIRTH, ************) VALUES (?,?,?,?,?,?)");

                 st.setInt(1,0);
                 st.setString(2,userName1);
                 st.setString(3,password);
                 st.setString(4,eaddress);
                 st.setDate(5,age);
                 st.setInt(6,************);


        st.executeUpdate();
                st.clearParameters();


        conn.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }


%>

</body>
</html>


Reply With Quote