170 likes | 174 Views
Learn how to use JDBC to connect to databases, query data, and display results in a JTable. This tutorial covers the basics of JDBC, including setting up the driver, creating a connection, executing queries, and displaying results.
E N D
Week 14 Accessing Databases with JDBC MSIS 655Advanced Business Applications Programming 14.1
Introduction • RDBMS • Relational database management system • MySQL • Open source • Available for both Windows and Linux • http://dev.mysql.com/downloads/mysql/5.0.html • JDBC • Java Database Connectivity • JDBC driver • Enable Java applications to connect to database • Enable programmers to manipulate databases using JDBC • http://dev.mysql.com/downloads/connector/j/5.0.html
Manipulating Databases with JDBC • Connect to a database • Query the database • Display the results of the query in JTable
Loads the class definition for the database driver. Declare and initialize a Connection reference called connection. Imports the JDBC classes and interfaces from package java.sql Declare a String constant that specifies the JDBC driver’s class name Declare a String constant that specifies the database URL
Extract the contents of one column in the current row Obtain column name using method getColumnName Use the Statement object’s executeQuery method to execute a query that selects all the author information from table authors.
Querying the books Database • Allow the user to enter any query into the program • Display the results of a query in a JTable