Found 4336 Articles for Java 8

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

255 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

328 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

447 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

585 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

613 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

699 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

How to add a new column to an existing table using JDBC API?

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

2K+ Views

You can add a new column to a table using the ALTER TABLE command.SyntaxALTER TABLE table_name ADD column_name datatype;Assume we have a table named Sales in the database with 5 columns namely ProductName, CustomerName, DispatchDate, DeliveryTime, Price and, Location as shown below:+-------------+--------------+--------------+--------------+-------+----------------+ | ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location       | +-------------+--------------+--------------+--------------+-------+----------------+ | Key-Board   | Raja         | 2019-09-01   | 08:51:36     | 7000  | Hyderabad      | | Earphones   | Roja         | 2019-05-01   | 05:54:28     | 2000 ... Read More

How to convert a String into a Date object using JDBC API?

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

1K+ Views

The valueOf() method of the Date object accepts a String value representing a Date in JDBC escape format i.e. yyyy-mm-dd and converts the given String value into java.sql.Date object.Date date = Date.valueOf(“date_string”);Assume we have created a table named employee_data with the description as shown below:+----------+--------------+------+-----+---------+-------+ | Field    | Type         | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+-------+ | id       | int(11)      | YES  |     | NULL    | | | Name     | varchar(255) | YES  |     ... Read More

How to convert a Date value to string in JDBC?

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

1K+ Views

The toString() method of the java.sql.Date class returns the escape format: yyyy-mm-dd of the date represented by the current date object. Using this method you can convert a Date object to a String.Date date = rs.getDate("Dispatch_Date"); date.toString());Assume we have a table named dispatch_data 3 records as shown below:+--------------+------------------+---------------+----------------+ | Product_Name | Name_Of_Customer | Dispatch_Date | Location | +--------------+------------------+---------------+----------------+ | KeyBoard     | Amith            | 1981-12-05    | Hyderabad | | Ear phones   | Sumith           | 1981-04-22   ... Read More

How to convert a Date object in to Timestamp in JDBC program?

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

526 Views

The getTime() method of the java.sql.Date class retrieves and returns the time from the current timestamp in milliseconds (long) from epoch time 1, 1970 00:00:00.000 GMT.//Retrieving the date Date date = rs.getDate("Dispatch_Date");The constructor of the java.sql.Timestamp class accepts a long variable representing the time in milliseconds from the epoch time and constructs the Timestamp object.//Creating a Timestamp object. Timestamp ts = new Timestamp(date.getTime()));Using these, you can convert a Date object to TimeStamp object in JDBC.Assume we have established connection with MySQL database and created a table named dispatch_data using statement object as:Assume we have established connection with MySQL database and ... Read More

Advertisements