Found 317 Articles for JDBC

How to get the number of records in a table using JDBC?

Nitya Raut
Updated on 30-Jul-2019 22:30:25

362 Views

The ResultSet class doesn’t provide any direct method to get the number of records in a table.The beforeFirst() method navigates the pointer/curser of the ResultSet object to its default position before first.In the same way the last() method positions the cursor at the last row of the ResultSet object.Using these methods you can find the number of records in the current ResultSet object.ExampleAssume we have a table named customers table with contents as shown below:+----+---------+-----+---------+----------------+ | ID | NAME    | AGE | SALARY  | ADDRESS        | +----+---------+-----+---------+----------------+ | 1  | Amit    | 25  | 3000.00 ... Read More

How to create and release a save point in JDBC?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

738 Views

When you set a save point you define a logical rollback point within a transaction. If an error occurs past a save point, you can use the rollback method to undo either all the changes or only the changes made after the save point.Savepoint interface gives you the additional transactional control. Most modern DBMS, support save points within their environments such as Oracle's PL/SQL.Setting a save pointYou can set a save point in a database using the setSavepoint(String savepointName) method of the Connection interface, this method accepts a string value representing the name of the save point and returns a ... Read More

How to set/insert null values to in to a column of a row using JDBC program?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

6K+ Views

You can insert null values into a table in SQL in two ways:Directly inserting the value NULL into the desired column as:Insert into SampleTable values (NULL);Using ‘ ’ as nullInsert into SampleTable values (NULL);While inserting data into a table using prepared statement object you can set null values to a column using the setNull() method of the PreparedStatement interface.pstmt.setNull(parameterIndex, sqlType);ExampleAssume we have a table named cricketers_data in the database with the following contents:+------------+------------+---------------+----------------+-------------+ | First_Name | Last_Name  | Date_Of_Birth | Place_Of_Birth | Country | +------------+------------+---------------+----------------+-------------+ | Shikhar    | Dhawan     | 1981-12-05    | ... Read More

How to set auto-increment to an existing column in a table using JDBC API?

Nitya Raut
Updated on 30-Jul-2019 22:30:25

513 Views

You can add/set an auto increment constraint to a column in a table using the ALTER TABLE command.SyntaxALTER TABLE table_name ADD id INT PRIMARY KEY AUTO_INCREMENTAssume we have a table named Dispatches in the database with 7 columns namely id, CustomerName, DispatchDate, DeliveryTime, Price and, Location with description as shown below:+--------------+--------------+------+-----+---------+-------+ | Field        | Type         | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | ProductName  | varchar(255) | YES  | UNI | NULL    | | | CustomerName | varchar(255) | YES  |     ... Read More

How to drop constraint on a column of a table in a database using JDBC API?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

254 Views

You can drop a constraint on a column of a table using the ALTER TABLE command.SyntaxALTER TABLE table_name DROP CONSTRAINT MyUniqueConstraint;Assume we have a table named Dispatches in the database with 7 columns namely id, CustomerName, DispatchDate, DeliveryTime, Price and, Location with description as shown below:+--------------+--------------+------+-----+---------+-------+ | Field        | Type         | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | ProductName  | varchar(255) | YES  | UNI | NULL    | | | CustomerName | varchar(255) | YES  |     | NULL    | ... Read More

How to add a NOT NULL constraint to a column of a table in a database using JDBC API?

Nitya Raut
Updated on 30-Jul-2019 22:30:25

327 Views

You can add a not null constraint to a column of a table using the ALTER TABLE command.SyntaxALTER TABLE table_name MODIFY column_name datatype NOT NULL;Assume we have a table named Dispatches in the database with 7 columns namely id, CustomerName, DispatchDate, DeliveryTime, Price and, Location with description as shown below:+--------------+--------------+------+-----+---------+-------+ | Field        | Type         | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | ProductName  | varchar(255) | YES  |     | NULL    |       | | CustomerName | varchar(255) | YES  |     | NULL   ... Read More

How add a unique key constraint to a column of a table in a database using JDBC API?

Nitya Raut
Updated on 30-Jul-2019 22:30:25

444 Views

You can add a unique constraint to a column using the ALTER TABLE commandSyntaxALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...);Assume we have a table named Dispatches in the database with 7 columns namely id, CustomerName, DispatchDate, DeliveryTime, Price and, Location with description as shown below:+--------------+--------------+------+-----+---------+-------+ | Field        | Type         | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | ProductName  | varchar(255) | YES  |     | NULL    | | | CustomerName | varchar(255) | No   |     | NULL ... Read More

How to add a primary key constraint to a column of a table in a database using JDBC API?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

584 Views

You can add a primary key constraint to a column of a table using the ALTER TABLE command.SyntaxALTER TABLE table_name ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (column1, column2...);Assume we have a table named Dispatches in the database with 7 columns namely id, CustomerName, DispatchDate, DeliveryTime, Price and, Location with description as shown below:+--------------+--------------+------+-----+---------+-------+ | Field        | Type         | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | ProductName  | varchar(255) | YES  |     | NULL | | | CustomerName | varchar(255) | ... Read More

How to delete a column from an existing table in a database using JDBC API?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

606 Views

You can delete a column in a table using the ALTER TABLE command.SyntaxALTER TABLE table_name DROP COLUMN column_name;Assume we have a table named Sales in the database with 7 columns namely id, CustomerName, DispatchDate, DeliveryTime, Price and, Location as shown below:+----+-------------+--------------+--------------+--------------+-------+----------------+ | id | productname | CustomerName | DispatchDate | DeliveryTime | Price | Location     | +----+-------------+--------------+--------------+--------------+-------+----------------+ | 1  | Key-Board   | Raja         | 2019-09-01   | 08:51:36     | 7000  | Hyderabad      | | 2  | Earphones   | Roja         | 2019-05-01   ... Read More

How to change the datatype of a column in an existing table using JDBC API?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

696 Views

You can change the datatype of a column in a table using the ALTER TABLE command.SyntaxALTER TABLE Sales MODIFY COLUMN column_name column_new_datatuypeAssume we have a table named Sales in the database with 7 columns namely ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location and, ID with description as:+--------------+--------------+------+-----+---------+-------+ | Field        | Type         | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | ProductName  | varchar(255) | YES  |     | NULL | | | CustomerName | varchar(255) | YES  |     | NULL ... Read More

Advertisements