I have error in my code. I am trying to do a simple database insert program using javabeans and jsp. I get a value to be inserted in database through the jsp page in a text box and would like to be inserted into database using beans The connection to database and mysql query are in java file.
Here is the code.
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page language="Java" import="java.sql.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<html>
<head>
</head>
<body>
<form name="form1" action="beancode" method="POST">
Emp ID: <input type="text" name ="emplid"> <br><br><br>
<input type = "submit" value="Submit">
<jsp:useBean id="sampl" class="beancode" scope="page">
<jsp:setProperty name="sampl" property="*"/>
</jsp:useBean>
</form>
</body>
</html>
I know i might have made a mistake here in using the bean. Here is the java code which does the insert part.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
*
* @author Trainees
*/
public class beancode
{
private String employid;
private Connection con = null;
private ResultSet rs = null;
private PreparedStatement st = null;
/** Creates a new instance of beancode */
public beancode()
{
try
{
Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample?user=user&password=password");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public void setemployid(String empid)
{
employid = empid;
}
public String getemployid()
{
return (employid);
}
public void insert()
{
try
{
String s1="insert into samp values('"+employid+"')";
st = con.prepareStatement(s1);
st.executeUpdate();
st.clearParameters();
st.close();
}
catch(Exception m)
{
//
}
}
}
If anyone could help me out with where i went wrong in the code and if you could help me with corrections in it. Would be of real great help to me. Thanks in advance.