Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Engineering for Internet Applications Lab 4 Tooba Aamir - 396447

University of Melbourne (Semester-2 2011) Department of Computer Science and Software Engineering

Course title Engineering for Internet Applications

Submitted by: Tooba Aamir 396447

Dated September, 2 2011

Engineering for Internet Applications Lab 4 Tooba Aamir - 396447 1. Manually display the contents of your database/table

Engineering for Internet Applications Lab 4 Tooba Aamir - 396447 2. Copy-and-paste the output of your program for some test cases.
Test cases include first 'insert' of URL ID 5 then 'list' of all table and at end delete' of URL ID 5.

3. Include your entire source code in your report.


import import import import java.sql.*; java.io.BufferedReader; java.io.IOException; java.io.InputStreamReader;

public class Connect { public static void main(String[] args) { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); System.out.println("MySQL Connect Example."); Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "URLCollection"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "ciit"; String c = "1"; try { Class.forName(driver); conn = DriverManager.getConnection(url+dbName,userName,password);

Engineering for Internet Applications Lab 4 Tooba Aamir - 396447


System.out.println("Connected to the database"); //------Statement s = conn.createStatement (); while(!(c.equals("0"))) { System.out.println ("Enter '1' to list all URLs\t'2' to delete URl\t'3' to insert URL\t'0' to to quit"); String st = br.readLine(); c = st; if (st.equals("1")) { s.executeQuery ("SELECT ID, url, EnterAt FROM urls"); ResultSet rs = s.getResultSet (); while (rs.next ()) { String IDVal = rs.getString ("ID"); String nameVal = rs.getString ("url"); String catVal = rs.getString ("EnterAt"); System.out.println (IDVal + ": URL = " + nameVal + ", Entry Date = " + catVal); } rs.close (); } else if (st.equals("2")) { System.out.println ("Enter URL ID to delete"); String st2 = br.readLine(); int delete = s.executeUpdate("DELETE FROM urls WHERE ID='"+st2+"'"); } else if (st.equals("3")) { System.out.println ("Enter URL ID"); String st2 = br.readLine(); System.out.println ("Enter URL"); String st3 = br.readLine(); System.out.println ("Enter date (yy/mm/dd)"); String st4 = br.readLine(); int insert = s.executeUpdate("INSERT INTO urls VALUES ('"+st2+"','"+st3+"','"+st4+"')"); } } s.close (); //------conn.close(); System.out.println("Disconnected from database"); } catch (Exception e) { e.printStackTrace(); } } }

You might also like