You are on page 1of 5

S517 Lab Database Operation

Task 1: Browse existing DB In this task, you will create a Java project (Dynamic Web Project) to connect MySql database. This project will provide an easy web interface to search in a database table and return the relevant results. First, create a Dynamic Web Project as pervious labs, and then create a java Servlet class named like DBsearch. Second, you need to import a complied java library for MySql database connection. You can download this Jar file at: mysql-connector-java5.0.7-bin.jar and save this file in a folder. Then, right click the project name in the Project Explorer, and choose Properties. As following diagram shows, click Java Build Path, select Libraries tab, and click Add External JARs button. In the browse window, select the downloaded Jar file and click OK to continue.

Meanwhile, you will have to cope and paste this Jar file into the {project folder}/WebContent/WEB-INF/lib folder and this will be used by Java Servlet. After finish this, you can click the project and press F5 to refresh the project content.

Third, you need to write some code to connect to a test database. In order to start, you need to import necessary classes as following:
import import import import import java.sql.Connection; java.sql.DriverManager; java.sql.SQLException; java.sql.Statement; java.sql.ResultSet;

Then, please past the following code to replace the class code:
public class DBconnect extends HttpServlet { private static final long serialVersionUID = 1L; Connection connection; Statement statement = null; /** * @see HttpServlet#HttpServlet() */ public DBconnect() { super(); // TODO Auto-generated constructor stub } public void connect () throws Exception { Class.forName("org.gjt.mm.mysql.Driver"); String url = "jdbc:mysql://rdc04.uits.iu.edu:3264/S517DB"; String username = "S517"; String password = "S517"; connection = DriverManager.getConnection (url, username, password); statement = connection.createStatement(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); out.println("<html><head>"); //Start to print HTML page out.println("<title>DB connection</title></head><body>"); out.println("<h2>Please click this button and get the DB results</h2>"); //Create a HTML form with post method out.println("<form method=\"post\" action =\"" + request.getContextPath() + "/DBconnect\" >"); out.println("<table border=\"0\"><tr><td valign=\"top\">"); out.println("</td></tr><tr><td valign=\"top\">"); out.println("<input type=\"submit\" value=\"Show results\"></td></tr>"); out.println("</table></form>"); out.println("</body></html>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Result page response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); out.println("<html><head>"); out.println("<title>DB Result</title></head><body>"); out.println("<h2>DB results:</h2>"); out.println("ID\tFirstname\tLastname\tAge\tGender<br>");

//Output try { connect(); ResultSet rs = statement.executeQuery("select * from user"); //Select all the records from user table while (rs.next()) { //Read each record int ID = rs.getInt("ID"); String firstname = rs.getString("Firstname"); String lastname = rs.getString("Lastname"); int age = rs.getInt("Age"); String gender = rs.getString("gender"); out.println(ID + "\t" + firstname + "\t"+ lastname + "\t" + age + "\t" + gender + "\t<br>"); } rs.close(); statement.close(); connection.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } out.println("</body></html>"); } }

Please focusing on the global variables


Connection connection; Statement statement = null;

Which can be used for this class for DB connection and DB statement. There is another method you will use later as following:
public void connect () throws Exception { Class.forName("org.gjt.mm.mysql.Driver"); String url = "jdbc:mysql://rdc04.uits.iu.edu:3264/S517DB"; String username = "S517"; String password = "S517"; connection = DriverManager.getConnection (url, username, password); statement = connection.createStatement(); }

Which tells the DB connection information. The url variable declares the remote DB location as jdbc:mysql://rdc04.uits.iu.edu. 3264 is this string is the port number. S517DB is the database name. Meanwhile, username and password string can be found at the corresponding variables. Read doGet and doPost program carefully, and write down the functionality of these functions: doGet: _______________________________________________________________________ __________________ doPost: _______________________________________________________________________

_______________________________________________________________________ _______________________________________________________________________ _______________________________________________________________________ _______________________________________________________________________ _________________________________ Tips: in doPost method, ResultSet rs = statement.executeQuery("select * from user"); will open the following table and save all the records into the variable rs (rs saved all the records of user table) DBname: S517DB Table Firstname Lastname George Ryan Kenneth Armstrong Amy Archer Don Anderson Jenny Crawford Virginia Allen Billy Rice James Rostetter Paul Ropp Mary Drexler Jenny Cox name: user Age 23 52 18 25 26 19 62 26 73 32 45

ID 1 2 3 4 5 6 7 8 9 10 11

Gender M M F M F F M M M F F

Task 2: search user age from DB In this task, you will create a Java Servlet class, and this class will provide a very basic interface. Allow user to input an age, and the system will return all the records match this input age. i.e. ResultSet rs = statement.executeQuery("select * from user where age = " + searchage); Where searchage is the variable read from user input interface. After implement this, think about how to improve this interface? For instance, when user cannot find any matched records, system will print a message like Sorry, no result found! Pay attention! You need to close the database connection, database statement, and database recordset. Paste your code here:

doGet: doPost:

You might also like