In this post you will learn how to Establish a Database connection using JSP.The first thing you need to add is a page directive to import the java.sql package for the database code as follows:
You'll use a single ResultSet object to hold your database results. Because you'd like this to be
available to the entire page, the next thing you do is declare it in a JSP declaration tag:
Finally, you establish a connection to the database and retrieve the required data.
Now i will show you a complete code for simple application to connect database.
Employee Details
try {
Class.forName("org.gjt.mm.mysql.Driver");
Connection db = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/quoting");
Statement s = db.createStatement();
rs = s.executeQuery("select * from employee");
}
catch (Exception e) {
// For now, just report the error to the system log
System.out.println(e.toString());
}
%>
Output:
You'll use a single ResultSet object to hold your database results. Because you'd like this to be
available to the entire page, the next thing you do is declare it in a JSP declaration tag:
Finally, you establish a connection to the database and retrieve the required data.
Now i will show you a complete code for simple application to connect database.
try {
Class.forName("org.gjt.mm.mysql.Driver");
Connection db = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/quoting");
Statement s = db.createStatement();
rs = s.executeQuery("select * from employee");
}
catch (Exception e) {
// For now, just report the error to the system log
System.out.println(e.toString());
}
%>
Employee Details | |||||
Employees
| |||||
New Employee |
Output: