import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;

public class InsertInTable extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		res.setContentType("text/html");
		PrintWriter pw = res.getWriter();
		Statement stmt;
		Connection con;
		ResultSet rs;
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			con = DriverManager.getConnection("jdbc:odbc:dataInsert");
			stmt = con.createStatement();
			int i = stmt.executeUpdate("insert into data values ('Bipul', 24)");
			if (i != 0) {
				pw.println("Record has been inserted successfully<br>");
			} else {
				pw.println("Inserting record get failure");
			}
			rs = stmt.executeQuery("SELECT * FROM data");
			pw.println("name\t age");
			while (rs.next()) {
				pw.println("<br>" + rs.getString(1) + ",\t " + rs.getInt(2));
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			pw.println(e.getMessage());
		}
	}
}
