Found 317 Articles for JDBC

Java ResultSetMetaData getColumnName() method with example

Rishi Raj
Updated on 30-Jul-2019 22:30:26

931 Views

The getColumnName() method of the ResultSetMetaData (interface) retrieves and returns the name of the specified column in the current ResultSet object.This method accepts an integer value representing the index of a column and, returns a String value representing the name of the specified column.To get the ResultSetMetaData object, you need to −Register the Driver: Select the required database register the Driver class of the particular database using the registerDriver() method of the DriverManager class or, the forName() method of the class named Class.DriverManager.registerDriver(new com.mysql.jdbc.Driver());Get connection: Create a connection object by passing the URL of the database, username and password of a ... Read More

How to get all the column names from a ResultSet using JDBC

Rishi Raj
Updated on 30-Jul-2019 22:30:26

13K+ Views

You can get the name of a particular column using the getColumnName() method of the ResultSetMetadata interface.This method accepts an integer value representing the index of a column and returns a String value representing the name of the specified column.Let us create a table with name MyPlayers in MySQL database using CREATE statement as shown below −CREATE TABLE MyPlayers(    ID INT,    First_Name VARCHAR(255),    Last_Name VARCHAR(255),    Date_Of_Birth date,    Place_Of_Birth VARCHAR(255),    Country VARCHAR(255),    PRIMARY KEY (ID) );Now, we will insert 7 records in MyPlayers table using INSERT statements −insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'); insert into MyPlayers ... Read More

How to get the list of all databases using JDBC?

Vikyath Ram
Updated on 30-Jul-2019 22:30:26

2K+ Views

You can get the list of databases in MySQL using the SHOW DATABASES query.show databases;Following JDBC program retrieves the list of databases by executing the show databases query.Exampleimport java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class ShowDatabasesExample {    public static void main(String args[]) throws Exception {       //Registering the Driver       DriverManager.registerDriver(new com.mysql.jdbc.Driver());       //Getting the connection       String mysqlUrl = "jdbc:mysql://localhost/mydatabase";       Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");       System.out.println("Connection established......");       //Creating a Statement object       Statement stmt = con.createStatement(); ... Read More

How to Maintain an open ResultSet after commit in JDBC?

Arushi
Updated on 30-Jul-2019 22:30:26

443 Views

ResultSet holdability determines whether the ResultSet objects (cursors) should be closed or held open when a transaction (that contains the said cursor/ ResultSet object) is committed using the commit() method of the Connection interface.ResultSet interface provides two values to specify the holdability namely CLOSE_CURSORS_AT_COMMIT and HOLD_CURSORS_OVER_COMMITIf the holdability of the ResultSet object is set to this value. Whenever you commit/save a transaction using the commit() method of the Connection interface, the ResultSet objects created in the current transaction (that are already opened) will be held open.Therefore, if you need to hold the ResultSet cursor open after the commit automatically, set ... Read More

How to close the ResultSet cursor automatically, after commit in JDBC?

Rishi Raj
Updated on 30-Jul-2019 22:30:26

802 Views

ResultSet holdability determines whether the ResultSet objects (cursors) should be closed or held open when a transaction (that contains the said cursor/ ResultSet object) is committed using the commit() method of the Connection interface.ResultSet interface provides two values to specify the holdability namely CLOSE_CURSORS_AT_COMMIT and HOLD_CURSORS_OVER_COMMITIf the holdability of the ResultSet object is set to this value. Whenever you commit/save a transaction using the commit() method of the Connection interface, the ResultSet objects created in the current transaction (that are already opened) will be closed.Therefore, if you need to close the ResultSet cursor after the commit automatically, set the ResultSet ... Read More

How to get all table names from a database using JDBC?

Arushi
Updated on 30-Jul-2019 22:30:26

6K+ Views

You can get the list of tables in the current database in MySQL using the SHOW TABLES query.Show tables;Following JDBC program retrieves the list of tables in the database by executing the show tables query.Exampleimport java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class ListingTables {    public static void main(String args[]) throws Exception {       //Registering the Driver       DriverManager.registerDriver(new com.mysql.jdbc.Driver());       //Getting the connection       String mysqlUrl = "jdbc:mysql://localhost/mydatabase";       Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");       System.out.println("Connection established......");       //Creating a Statement object ... Read More

How do you check if a ResultSet is closed or not in JDBC?

Arushi
Updated on 30-Jul-2019 22:30:26

2K+ Views

Whenever we execute SQL statements using the executeQuery() method, it returns a ResultSet object which holds the tabular data returned by the SELECT queries(in general).The ResultSet object contains a cursor/pointer which points to the current row. Initially this cursor is positioned before first row (default position).The isClosed() method of the ResultSet interface is used to determine whether the current ResultSet object is closed.rs.isclosed()Let us create a table with name tutorials_data in MySQL database using CREATE statement as shown below −CREATE TABLE tutorials_data (    tutorial_id INT,    tutorial_title VARCHAR(100),    tutorial_author VARCHAR(40),    submission_date date,    PRIMARY KEY (tutorial_id) );Now, we will insert 5 ... Read More

How do you check if a ResultSet is empty or not in JDBC?

Vikyath Ram
Updated on 21-Feb-2020 10:51:41

6K+ Views

Whenever we execute SQL statements using the executeQuery() method, it returns a ResultSet object which holds the tabular data returned by the SELECT queries(in general).The ResultSet object contains a cursor/pointer which points to the current row. Initially this cursor is positioned before first row (default position).The next() methodThe next() method of the ResultSet interface moves the pointer of the current (ResultSet) object to the next row, from the current position.This method returns a boolean value specifying whether the ResultSet object contains more rows. If there are no rows next to its current position this method returns false, else it returns true.Therefore, ... Read More

How can you move the cursor in scrollable result sets in JDBC?

Rishi Raj
Updated on 30-Jul-2019 22:30:26

413 Views

In JDBC there are two scrollable ResultSets namely, scrollable sensitive and, scrollable insensitive.In the TYPE_SCROLL_INSENSITIVE ResultSet the cursor moves in forward or backward directions. This type of ResultSet is insensitive to the changes that are made in the database i.e. the modifications done in the database are not reflected in the ResultSet.Which means if we have established a connection with a database using JDBC program and retrieved a ResultSet holding all the records in a table named SampleTable. Meanwhile if we have added some more records to the table (after retrieving getting the ResultSet), these recent changes will not be reflected ... Read More

How to move the ResultSet cursor to the first row in JDBC?

Vikyath Ram
Updated on 30-Jul-2019 22:30:26

4K+ Views

Whenever we execute SQL statements using the executeQuery() method, it returns a ResultSet object which holds the tabular data returned by the SELECT queries(in general).The ResultSet object contains a cursor/pointer which points to the current row. Initially this cursor is positioned before first row (default position).You can move the cursor of the ResultSet object to the first row from the current position, using the first() method of the ResultSet interface.rs.first()This method returns a boolean value specifying whether the cursor has been moved to the first row successfully.If there are no rows in the current ResultSet object this method returns false, else it ... Read More

Previous 1 ... 5 6 7 8 9 ... 32 Next
Advertisements