CREATE TABLE `mohan2` (
`sno` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL,
`sal` int(30) NOT NULL,
`eid` varchar(30) NOT NULL,
PRIMARY KEY (`sno`,`eid`)
)
1)
<%--index.jsp--%>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="insert.jsp" method=post>
<center><b>Name:</b><input type="text" name="t1"><br><br>
<b>Salary:</b><input type="text" name="t2"><br><br>
<input type="submit" value="Submit">
</center>
</form>
</body>
</html>
2)
<%-- insert.jsp--%>
<%@ page language="java" import="java.sql.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'insert.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<%!
public int Generate(String name1,int t)
{
int len=5;
int x=0;
Connection conn = null;
Statement st=null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "MYSQL";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "password";
String eid="";
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
st=conn.createStatement();
ResultSet rs=st.executeQuery("select max(sno) from mohan2");
rs.next();
int s=rs.getInt(1);
s=s+1;
StringBuffer sb = new StringBuffer(len);
sb.append("P");
len=len-1;
String v=String.valueOf(s);
int p=v.length();
len=len-p;
while(len--!=0)
sb.append("0");
sb.append(v);
eid=sb.toString();
rs.close();
st.close();
st=conn.createStatement();
x=st.executeUpdate("insert into mohan2(eid,name,sal) values('"+eid+"','"+name1+"',"+t+")");
st.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return x;
}
%>
<body>
<%
String name1=request.getParameter("t1");
String sal1=request.getParameter("t2");
int t=Integer.parseInt(sal1);
int b=Generate(name1,t);
if(b==1)
out.println("<h2>Record Successfully inserted</h2>");
else
out.println("<h2>Failed</h2>");%>
</body>
</html>
any body want to employee id automatically generate please try this code, useful for u
thanks
CREATE TABLE mohan2 ( sno bigint(20) NOT NULL AUTO_INCREMENT, name varchar(30) NOT NULL, sal int(30) NOT NULL, eid varchar(30) NOT NULL, PRIMARY KEY (sno,eid) )
1)index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="insert.jsp" method=post>
<center><b>Name:</b><input type="text" name="t1"><br><br>
<b>Salary:</b><input type="text" name="t2"><br><br>
<input type="submit" value="Submit">
</center>
</form>
</body>
</html>
2)insert.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'insert.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<%!
public int Generate(String name1,int t)
{
int len=5;
int x=0;
Connection conn = null;
Statement st=null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "MYSQL";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "password";
String eid="";
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
st=conn.createStatement();
ResultSet rs=st.executeQuery("select max(sno) from mohan2");
rs.next();
int s=rs.getInt(1);
s=s+1;
StringBuffer sb = new StringBuffer(len);
sb.append("P");
len=len-1;
String v=String.valueOf(s);
int p=v.length();
len=len-p;
while(len--!=0)
sb.append("0");
sb.append(v);
eid=sb.toString();
rs.close();
st.close();
st=conn.createStatement();
x=st.executeUpdate("insert into mohan2(eid,name,sal) values('"+eid+"','"+name1+"',"+t+")");
st.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return x;
}
%>
<body>
<%
String name1=request.getParameter("t1");
String sal1=request.getParameter("t2");
int t=Integer.parseInt(sal1);
int b=Generate(name1,t);
if(b==1)
out.println("<h2>Record Successfully inserted</h2>");
else
out.println("<h2>Failed</h2>");%>
</body>
</html>