how to insert data into two tables from a single page
Dear everyone,
I have a task, in which the datas to be inserted into two tables...
ie in first table, i have
***************
* employee id
*employee name
*employee password
i have second table, i have
**********************
* employee id
*skills
primary key is: employee id
***********************************
how insert the employee id in both table one and second....
i will show u the code, i have written for one table , how to do for two table from a single page
<%
String s1=request.getParameter("eid");
String s2=request.getParameter("ename");
String s3=request.getParameter("epass");
String s4=request.getParameter("email");
%>
<%
int i=0;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:empl");
PreparedStatement prep=con.prepareStatement("insert into emp values(?,?,?,?)");
prep.setString(1,s1);
prep.setString(2,s2);
prep.setString(3,s3);
prep.setString(4,s4);
i=prep.executeUpdate();
//con.close();
}
catch(Exception e)
{
}
%>
# 2
thank u for kind suggestions..
is this correct
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:empl");
PreparedStatement prep=con.prepareStatement("insert into emp values(?,?,?,?) lang values(?)");
prep.setString(1,s1);
prep.setString(2,s2);
prep.setString(3,s3);
prep.setString(4,s4);
i=prep.executeUpdate();
Connection lan=DriverManager.getConnection("jdbc:odbc:langskill");
PreparedStatement lanu=con.prepareStatement("insert into lang values(?)");
lanu.setString(1,s1);
}
catch(Exception e)
{
}
# 6
i still followed the same way, but second table not getting updated
i will show u the code, please give ur suggestion sir...
<%
String s1=request.getParameter("eid");
String s2=request.getParameter("ename");
String s3=request.getParameter("epass");
String s4=request.getParameter("email");
%>
<%
int i=0;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:empl");
Connection lan=DriverManager.getConnection("jdbc:odbc:langskill");
PreparedStatement prep=con.prepareStatement("insert into emp values(?,?,?,?) ");
prep.setString(1,s1);
prep.setString(2,s2);
prep.setString(3,s3);
prep.setString(4,s4);
PreparedStatement lanu=con.prepareStatement("insert into lang(eid) values(?)");
lanu.setString(1,s1);
i=prep.executeUpdate();
}
catch(Exception e)
{
}
%>
# 7
> i still followed the same way, but second table not
> getting updated
>
The wisdom of what you are doing aside how many executeUpdate()s do you see in the code you posted? I see one.
I also see exactly zero Statements and Connections being closed.
I also see a catch block with zero usefulness.
This is all very wrong and you should stop now.
# 8
You're preparing the second statement on the wrong connection.
And do not suppress exceptions. At least print the stacktrace to the log, is is *very* helpful by debugging. Try it out yourself by adding the following line while your connection is still wrong:} catch (Exception e) {
e.printStackTrace();
}
Also do not forget to close connections. Closing at least the connection is recommended, because they will not closed automatically. If you're going to create lot of connections in a short time, without closing them, then you're leaking resources until you cannot connect the DB anymore and you need to restart the Java running environment. This is not affordable in a production environment.