University of Cincinnati logo and link  
Connections
 
  UC ingotTo connect to the database, you must register the driver.
  • We will do this by loading the class dynamically.  We have to supply the name of the class to another class that will dynamically instantiate it.

  • Class.forName("com.mysql.jdbc.Driver").newInstance();
     
    • For our example, this is fine.  But more often than not, you'll see it pulled in from a file, and saved to a variable, let's say driver.

    • Class.forName(driver).newInstance();
    • Once you've done that, you can open a connection from this driver.

    • java.sql.Connection conn;
       
       conn = DriverManager.getConnection("jdbc:mysql://localhost/students");

      This line often includes username and password as well.

    • The driver class ("com.mysql.jdbc.Driver") must be available in the classpath.  We can just drop this right into Tomcat.
 Statements and Result Sets