

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of jdbc connections, focusing on the two-tier architecture, setup process, and usage of jdbc drivers, database connection objects, and executing sql statements. Learn how to download and add jdbc drivers, load them, and connect to a database using jdbc url syntax. Discover the differences between statement, prepared statement, and callable statement, and learn how to execute sql queries and update databases.
Typology: Slides
1 / 3
This page cannot be seen from the preview
Don't miss anything!


JDBC ConnectionCalin Curescu, parts from http://www.kitebird.com/articles/jdbc.html and http://www.jdbc- tutorial.com/
Calin Curescu, following http://www.kitebird.com/articles/jdbc.html and http://www.jdbc-tutorial.com/
12 pages JDBC ConnectionCalin Curescu, parts from http://www.kitebird.com/articles/jdbc.html and http://www.jdbc- tutorial.com/
2 of 12
JDBC Connection Calin Curescu, parts from http://www.kitebird.com/articles/jdbc.html and http://www.jdbc-tutorial.com/ 3 of 12
JDBC Connection Calin Curescu, parts from http://www.kitebird.com/articles/jdbc.html and http://www.jdbc-tutorial.com/ 4 of 12
JDBC Connection Calin Curescu, parts from http://www.kitebird.com/articles/jdbc.html and http://www.jdbc-tutorial.com/ 5 of 12
try { Class.forName(jdbc.DriverXYZ); }
catch(Exception x){ System.out.println( "Unable to load the driver class!" ); }
JDBC Connection Calin Curescu, parts from http://www.kitebird.com/articles/jdbc.html and http://www.jdbc-tutorial.com/ 6 of 12
try{ Connection dbConnection = DriverManager.getConnection(url, "loginName", "Password"); } catch( SQLException x ){ System.out.println( "Couldn't get connection!" ); }
JDBC ConnectionCalin Curescu, parts from http://www.kitebird.com/articles/jdbc.html and http://www.jdbc- tutorial.com/
7 of 12
JDBC ConnectionCalin Curescu, parts from http://www.kitebird.com/articles/jdbc.html and http://www.jdbc- tutorial.com/
8 of 12
Statement statement = dbConnection.createStatement();
JDBC Connection Calin Curescu, parts from http://www.kitebird.com/articles/jdbc.html and http://www.jdbc-tutorial.com/ 9 of 12
JDBC Connection Calin Curescu, parts from http://www.kitebird.com/articles/jdbc.html and http://www.jdbc-tutorial.com/ 10 of 12
Statement s = conn.createStatement (); int count; s.executeUpdate ("DROP TABLE IF EXISTS animal"); s.executeUpdate ( "CREATE TABLE animal (" + "id INT UNSIGNED NOT NULL AUTO_INCREMENT," + "PRIMARY KEY (id)," + "name CHAR(40), category CHAR(40))"); count = s.executeUpdate ( "INSERT INTO animal (name, category)" + " VALUES" + "('snake', 'reptile')," + "('frog', 'amphibian')," + "('tuna', 'fish')"); s.close (); System.out.println (count + " rows were inserted");
JDBC Connection Calin Curescu, parts from http://www.kitebird.com/articles/jdbc.html and http://www.jdbc-tutorial.com/ 11 of 12
JDBC Connection Calin Curescu, parts from http://www.kitebird.com/articles/jdbc.html and http://www.jdbc-tutorial.com/ 12 of 12
Statement s = conn.createStatement (); s.executeQuery ("SELECT id, name, category FROM animal"); ResultSet rs = s.getResultSet (); int count = 0; while (rs.next ()) { int idVal = rs.getInt ("id"); String nameVal = rs.getString ("name"); String catVal = rs.getString ("category"); System.out.println ( "id = " + idVal + ", name = " + nameVal + ", category = " + catVal); ++count; } rs.close (); s.close (); System.out.println (count + " rows were retrieved");