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.

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

String firstname = rs. String lastname = rs.println("</body></html>"). password).next()) { //Read each record int ID = rs. String url = "jdbc:mysql://rdc04.close().iu.println(ID + "\t" + firstname + "\t"+ lastname + "\t" + age + "\t" + gender + "\t<br>"). } } Please focusing on the global variables Connection connection. The url variable declares the remote DB location as “jdbc:mysql://rdc04. “3264” is this string is the port number.close().close().forName("org. username. String username = "S517". “S517DB” is the database name.executeQuery("select * from user").edu:3264/S517DB".iu.Driver"). String gender = rs. } rs.createStatement(). Read doGet and doPost program carefully. String password = "S517". //Select all the records from user table while (rs. statement = connection.getString("Firstname").getInt("Age").getConnection (url.getInt("ID"). There is another method you will use later as following: public void connect () throws Exception { Class. Statement statement = null.getString("gender"). } Which tells the DB connection information. } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(). Meanwhile. connection. } out.mm. and write down the functionality of these functions: doGet: _______________________________________________________________________ __________________ doPost: _______________________________________________________________________ . Which can be used for this class for DB connection and DB statement.uits. ResultSet rs = statement. connection = DriverManager.edu”. out. username and password string can be found at the corresponding variables. statement.uits.//Output try { connect().getString("Lastname").mysql.gjt. int age = rs.

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. and this class will provide a very basic interface. Paste your code here: . Allow user to input an age.executeQuery("select * from user")._______________________________________________________________________ _______________________________________________________________________ _______________________________________________________________________ _______________________________________________________________________ _________________________________ Tips: in doPost method. and database recordset. when user cannot find any matched records. think about how to improve this interface? For instance. system will print a message like “Sorry. Where searchage is the variable read from user input interface. no result found!” Pay attention! You need to close the database connection. ResultSet rs = statement.e. you will create a Java Servlet class. After implement this. database statement. ResultSet rs = statement.executeQuery("select * from user where age = " + searchage). and the system will return all the records match this input age. i.

doGet: doPost: .

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.