Mega Code Archive

 
Categories / Java Tutorial / JSP
 

Insert data to a table

<%@ page  import="java.sql.*" %> <HTML>     <HEAD>         <TITLE>Filling a Table</TITLE>     </HEAD>     <BODY>         <H1>Filling a Table</H1>         <%               Connection connection = null;             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");             connection = DriverManager.getConnection("jdbc:odbc:data", "YourName", "password");             Statement statement = connection.createStatement();             String command = "INSERT INTO Employees (ID, Name) VALUES (1, 'Tom')";             statement.executeUpdate(command);             command = "INSERT INTO Employees (ID, Name) VALUES (2, 'Peter')";             statement.executeUpdate(command);             ResultSet resultset = statement.executeQuery("select * from Employees");             while(resultset.next()){          %>             <TABLE BORDER="1">                 <TR>                     <TH>ID</TH>                     <TH>Name</TH>                 </TR>                 <TR>                     <TD> <%= resultset.getString(1) %> </TD>                     <TD> <%= resultset.getString(2) %> </TD>                 </TR>             </TABLE>         <%              }          %>     </BODY> </HTML>