This essay developed out of conversations I've had with several other programmers about why Java smelled suspicious. It's not a critique of Java!
Sycorax - complete tutorials
Programming Tutorials
" Java provides the industry - software companies and customer alike , an opportunity to create a true open computing environment where software is portable,
and customers benefit from increase competition. "
Java and the Future
December 1, 2008-LEJB 3.1: EJB New and Improved!
The EJB 3.0 specification was a huge improvement from what you were used to in the early versions of EJB. Available as an early draft, EJB 3.1 has many more features and is even easier to use.
December 1, 2008-Should Java Assert that Network I/O Can't Occur on the UI Thread?
Doing network I/O on the user interface (UI) thread is bad. Most developers know that and can tell you why; unfortunately, it's still done.
Register now to recieve special alert and latest technology news!
Servlet JDBC Connection
For this example there is one html page after entering the data when you will click on the submit button then one servlet will be called and from the servlet all the data will be collected and then the data will be inserted to the database.
index.html
<html>
<head>
<title>Sycorax Servlet JDBC Connection Example </title>
</head>
<body bgcolor=red>
<SCRIPT LANGUAGE="JavaScript">
<!--
function valid(form) {
var field1 = form.firstname;
var field2= form.lastname;
var field3= form.address;
var field4= form.phonenumber;
firstname=field1.value;
lastname=field2.value;
address=field3.value;
phonenumber=field4.value;
if (firstname.length==0)
{
alert("The first name cannot be left blank");
field1.focus();
field1.select();
return false;
}
else if (lastname.length==0)
{
alert("The lastname field cannot be left blank");
field2.focus();
field2.select();
return false;
}
else if (address.length==0)
{
alert("The address field cannot be left blank");
field3.focus();
field3.select();
return false;
}
else if (phonenumber.length==0)
{
alert("The phone number cannot be left blank");
field4.focus();
field4.select();
return false;
}
}
// -->
</SCRIPT>
<br>
<br>
<center>
<form onSubmit="return valid(this)" method=post action="MyServletAlias">
<table>
<tr>
<td>First Name: </td>
<td>
<input type=text name="firstname">
</td>
</tr>
<tr>
<td>Last Name: </td>
<td>
<input type=text name="lastname">
</td>
</tr>
<tr>
<td>Address: </td>
<td>
<input type=text name="address">
</td>
</tr>
<tr>
<td>Account Type: </td>
<td>
<input type=text name="account">
</td>
</tr>
<tr>
<td>Annual Income: </td>
<td>
<input type=text name="income">
</td>
</tr>
<tr>
<td>Phone Number: </td>
<td>
<input type=text name="phonenumber">
</td>
</tr>
</table>
<center><input type=SUBMIT value=SUBMIT></center>
</form>
</center>
</table>
</body>
</html>
MyServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.sql.*;
public class MyServlet extends HttpServlet
{
Connection dbcon;
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
// Establishing the connection with the database
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbcon=DriverManager.getConnection("jdbc:odbc:dbname","sa","password");
System.out.println("Connection established");
}catch(ClassNotFoundException e)
{
System.out.println("Database driver not found");
System.out.println(e.toString());
}
catch (Exception e)
{
System.out.println(e.toString());
} // end catch
// This program records the details in the Registration table
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String firstname=req.getParameter("firstname");
String lastname=req.getParameter("lastname");
String address=req.getParameter("address");
String accounttype=req.getParameter("account");
String income=req.getParameter("income");
String phonenumber=req.getParameter("phonenumber");
double annualincome=0.0;
Double temp=Double.valueOf(income);
annualincome=temp.doubleValue();
// inserting the values in the registration table
//-------------------------------------------
int rows=0;
try
{
PreparedStatement s=dbcon.prepareStatement("insert into registration(First_name,Last_name,Address,Phone_no) values(?,?,?,?)");
s.setString(1,firstname);
s.setString(2,lastname);
s.setString(3,address);
s.setString(4,phonenumber);
rows=s.executeUpdate();
}
catch (Exception e)
{
System.out.println(e.toString());
}
if (rows==0)
{
System.out.println("Error inserting data in the registration table");
}
else
{
System.out.println("The values have been inserted in the table successfully");
}
// Picking up the registration number from the table for the user
//----------------------------------------------------------------
String regno=new String(" ");
try
{
PreparedStatement s=dbcon.prepareStatement("select max(Registration_id) from registration");
ResultSet r=s.executeQuery();
r.next();
regno=r.getString(1);
}
catch(Exception e)
{
System.out.println(e.toString());
}
out.println("<html>");
out.println("<head><title>Sycorax Servlet JDBC Connection Example</title></head>");
out.println("<body bgcolor=pink>");
out.println("Thank you for registering in our Web site " + firstname);
out.println("<BR>");
out.println("Your registration ID is: <b> " +regno+"</b>");
out.println("<BR>");
out.println("</body></html>");
try
{
dbcon.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
web.xml
<web-app>
<display-name>First Example Applications</display-name>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServletAlias</url-pattern>
</servlet-mapping>
</web-app>