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)

{

}

%>

[1659 byte] By [senthil_yogaa] at [2007-11-27 4:11:37]
# 1
Just create two PreparedStatement instances, one for each table.By the way, don't suppress exceptions and don't forget to close at least the connection. You will lost crucial information and leaking resources otherwise ;)
BalusCa at 2007-7-12 9:17:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 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)

{

}

senthil_yogaa at 2007-7-12 9:17:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
One connection is sufficient.
BalusCa at 2007-7-12 9:17:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
but i created table 1 with one dsn nameand the second table with another dsn namewhat should i do?
senthil_yogaa at 2007-7-12 9:17:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
Ah I overlooked that. Yes, then that is the way.
BalusCa at 2007-7-12 9:17:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 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)

{

}

%>

senthil_yogaa at 2007-7-12 9:17:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 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.

cotton.ma at 2007-7-12 9:17:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 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.

BalusCa at 2007-7-12 9:17:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9
Reply to BalusC,it really worked and the datas got inserted in both the tables.i once again appreciate for your guidance,i have also applied the finally block for closing connectionsand also used printStakTrace to handle exceptionThank You very Much....
senthil_yogaa at 2007-7-12 9:17:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...