Database plays an important role in storing large amount of data in a pattern. Here we are going develop and example to access the database using Java and JDBC. For this, firstly we need to establish a connection between database and java file with the help of various types of APIs, interfaces and methods. We are using MySQL database.
Connection: This interface specifies connection with specific databases like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of this interface.
Class.forName(String driver): It loads the driver.
DriverManager: This class controls a set of JDBC drivers. Each driver has to be register with this class.
getConnection(String url, String userName, String password): This
method establishes a connection to specified database url. It is having three
url: - Database url where stored or created your database
username: - User name of MySQL
password: -Password of MySQL
getMetaData(): This is a method of Connection interface. It retrieves the metadata of the database.
DataBaseMetaData: This interface gives information about the database like number of tables in the database, columns of the table etc.
getTables(null, null, "%", null): This method provides the description of the tables available in the given catalog. As we have set other parameters null, so it will provide only table names.
Here is the code:
In this section we studies how to connect to database and then list all the tables in the database. We have used MySQL database server.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.