80 likes | 285 Views
Database Access. What is a database system?. Data (database). Programs that handle the data (database server). Programming language (SQL). Typical Configuration. How it’s used by a webserver?. CGI program realizes dbase needs to be accessed.
E N D
What is a database system? • Data (database). • Programs that handle the data (database server). • Programming language (SQL).
How it’s used by a webserver? • CGI program realizes dbase needs to be accessed. • It asks dbase server to get data (using SQL commands). • Dbase server returns data. • CGI program formats data in HTML and writes HTML to Browser.
Several ways a CGI program can interfere with Dbase system • Using a custom method used by that particular dbase server. • Using a standard access method like ODBC/JDBC. • Rolling the CGI program and dbase client into a single unit. • Placing the entire webserver inside the dbase server/client. (e.g., webserver is an Oracle OCI program).
JDBC access from a servlet • What is JDBC? • JDBC = Java DataBase Connectivity. • JDBC is really a Java class library (java.sql) that provides a standard way to access databases. • The idea is that, you can write code using JDBC, and not worry about the specifics of the database server. • Today, most database servers come with a JDBC component.
Sample servlet that access a database else { System.out.println ("Connection successful. Continuing ..."); } // Make a statement. Statement s = con.createStatement(); if (s == null) { System.out.println ("Statement creation unsuccessful"); System.exit(0); } else { System.out.println ("Statement creation successful ... continuing"); } // Execute the SQL statement. if ( s.execute (sql) ) { ResultSet r = s.getResultSet(); ResultSetMetaData meta = r.getMetaData(); int cols = meta.getColumnCount(); int rownum = 0; // Iterate over the rows - get one row at a time. while( r.next() ) { rownum++; System.out.println("Row: " + rownum); for(int i=0; i<COLS; body (?"); out.println (""); out.close(); // Screen I/O System.out.println ("Inside servlet ... servlet complete"); } public void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet (req, resp); } import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.net.*; import java.io.*; public class TestJDBCServlet extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Set the content type of the response. resp.setContentType ("text/html"); // Create a PrintWriter to write the response. java.io.PrintWriter out = new PrintWriter (resp.getOutputStream()); // The first part of the response. out.println (""); out.println (""); out.println (""); // The SQL query. String sql = "select * from emp"; System.out.println("Executing: " + sql); // Create a driver instance. try { Class.forName("com.imaginary.sql.msql.MsqlDriver").newInstance(); // Make a connection. String url = "jdbc:msql://localhost:8114/test"; Connection con = DriverManager.getConnection(url, "simha", ""); if (con == null) { System.out.println ("Connection unsuccessful"); System.exit(0); }