Found 317 Articles for JDBC

How to insert a DATALINK object into a table using JDBC?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

185 Views

A DATALINK object represents an URL value which refers to an external resource (outside the current database/data source), which can be a file, directory etc.You can store a DATALINK into an SQL table using the setURL() method of the PreparedStatement interface. This method accepts an integer value representing an index of the bind variable, an URL object and, inserts the given URL object in the column represented by the bind variable in the specified index.ExampleLet us create a table with name tutorials_data in MySQL database using CREATE statement as shown below −CREATE TABLE tutorials_data (    tutorial_id INT PRIMARY KEY ... Read More

Is it mandatory to register the driver while working with JDBC?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

506 Views

Initially, till Java6 it is needed to register the driver using Class.forname() or the registerDriver() method before establishing connection with the database.But, since Java 1.6, JDBC 4.0 API, there is no need to register the driver explicitly, You Just need to set the Class path for the JDBC 4.X driver, Java automatically detects the Driver class and loads it.ExampleIn the following JDBC program we are trying connect with MySQL database first of all include the dependency for the MySQL driver in the pom.xml of your project.    mysql    mysql-connector-java    8.0.16 Then, without registering the MySQL driver class com.mysql.jdbc.Driver ... Read More

How to get LocalDateTime object from java.sql.Date using JDBC?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

3K+ Views

The java.time package of Java8 provides a class named LocalDateTime is used to get the current value of local date and time. Using this in addition to date and time values you can also get other date and time fields, such as day-of-year, day-of-week and week-of-year.Converting java.sql.Date to LocalDateTimeThe java.sql.TimeStamp class provides a method with name toLocalDateTime() this method converts the current timestamp object to a LocalDateTime object and returns it.To convert date to LocalDateTime object.Create a Timestamp object from Date object using the getTime() method as −Date date = rs.getDate("DispatchDate"); //Converting Date to Timestamp Timestamp timestamp = new Timestamp(date.getTime());Now, ... Read More

How to start a transaction in JDBC?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

1K+ Views

A transaction is a unit of work that is performed against a database. Transactions are units or sequences of work accomplished in a logical order, whether in a manual fashion by a user or automatically by some sort of a database program.A transaction is the propagation of one or more changes to the database. For example, if you are creating a record or updating a record or deleting a record from the table, then you are performing a transaction on that table. It is important to control these transactions to ensure the data integrity and to handle database errors.Ending a ... Read More

How to run .SQL script using JDBC?

Nishtha Thakur
Updated on 29-Jun-2020 13:41:00

14K+ Views

A database script file is a file that contains multiple SQL quries separated from each other. Usually, these files have the .sql extention.Running .sql script files in JavaYou can execute .sql script files in Java using the runScript() method of the ScriptRunner class of Apache iBatis. To this method you need to pass a connection object.Therefore to run a script file −Register the MySQL JDBC Driver using the registerDriver() method of the DriverManager class.Create a connection object to establish connection with the MySQL database using the getConnection() method.Initialize the ScriptRunner class of the package org.apache.ibatis.jdbc.Create a Reader object to read ... Read More

How to use try-with-resources with JDBC?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

4K+ Views

Whenever, we instantiate and use certain objects/resources we should close them explicitly else there is a chance of Resource leak.Generally, we used close resources using the finally block as −Connection con = null; Statement stmt = null; ResultSet rs = null; //Registering the Driver try {    con = DriverManager.getConnection(mysqlUrl, "root", "password");    stmt = con.createStatement(); } catch (SQLException e) {    e.printStackTrace(); }   finally {    try {       rs.close();       stmt.close();       con.close();    } catch(SQLException e) {       e.printStackTrace();    } }From JSE7 onwards the try-with-resources statement is ... Read More

JDBC Class.forName vs DriverManager.registerDriver

Anvi Jain
Updated on 30-Jul-2019 22:30:26

3K+ Views

To connect with a database using JDBC you need to select get the driver for the respective database and register the driver. You can register a database driver in two ways −Using Class.forName() method − The forName() method of the class named Class accepts a class name as a String parameter and loads it into the memory, Soon the is loaded into the memory it gets registered automatically.Class.forName("com.mysql.jdbc.Driver");ExampleFollowing JDBC program establishes connection with MySQL database. Here, we are trying to register the MySQL driver using the forName() method.import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class RegisterDriverExample {    public static ... Read More

How do I check to see if a column name exists in a CachedRowSet in JDBC?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

2K+ Views

CachedRowSet interface does not provide any methods to determine whether a particular column exists.Therefore, to find whether a RowSet contains a specific column, you need to compare the name of each column in the RowSet with the required name. To do so −Retrieve the ResultSetMetaData object from the RowSet using the getMetaData() method.ResultSetMetaData meta = rowSet.getMetaData();Get the number of columns in the RowSet using the getColumnCount() method.int columnCount = meta.getColumnCount();The getColumnName() method returns the name of the column of the specified index. Using this method retrieve the column names of the RowSet from index 1 to column count and compare ... Read More

How to determine database type (name) for a given JDBC connection?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

2K+ Views

One way to get the name of the underlying database you have connected with is by invoking the getDatabaseProductName() method of the DatabaseMetaData interface. This method returns the name of the underlying database in String format.Therefore, to retrieve the name of your current database using Java code −Retrieve the DatabaseMetaData object of the current Connection using the getMetaData() method.//Retrieving the meta data object DatabaseMetaData metaData = con.getMetaData();Then, get the product name of the underlying database you have connected to using the getDatabaseProductName() method of the DatabaseMetaData interface as −//retrieving the name of the database String product_name = metaData.getDatabaseProductName();ExampleFollowing JDBC program ... Read More

Batch Inserts Using JDBC Statements

Anvi Jain
Updated on 30-Jul-2019 22:30:26

264 Views

Grouping a set of INSERT Statements and executing them at once is known as batch insert.Batch inserts using Statement objectTo execute a batch of insert statements using the Statement object −Add statements to the batch − Prepare the INSERT quires one by one and add them to batch using the addBatch() method of the Statement Interface as shown below −String insert1 = Insert into table_name values(value1, value2, value3, ......); stmt.addBatch(insert1); String insert2 = Insert into table_name values(value1, value2, value3, ......); stmt.addBatch(insert2); String insert3 = Insert into table_name values(value1, value2, value3, ......); stmt.addBatch(insert3);Execute the batch − After adding the required statements, ... Read More

Advertisements