|
How do I connect to my database with a JDBC connection?
Below is a sample program illustrating the connection routine that you will need to use for connecting to a MySQL database from a JSP/servlet application. <%@ page language="java" import="java.sql.*"%> <HTML> <HEAD> <TITLE>Simple JSP Example</TITLE> </HEAD> <BODY> <P> <% /* Connect with the database */ try { //loading the driver Class.forName("org.gjt.mm.mysql.Driver"); //making the connection Connection Conn = DriverManager.getConnection("jdbc:mysql://localhost/user_dbname" , "user_dbuser" , "password"); } catch(ClassNotFoundException e) { System.out.println("Driver not found."); System.out.println(e.toString()); throw new UnavailableException(this, "Class not found."); } catch(SQLException e) { System.out.println("An error occurs."); System.out.println(e.toString()); throw new UnavailableException(this, "Cannot connect with the specified database."); } %> </P> </BODY> </HTML> support@mercuryd.com |