Found 317 Articles for JDBC

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

522 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

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

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

451 Views

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

How to get the row count in JDBC?

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

5K+ Views

The SQL Count() function returns the number of rows in a table. Using this you can get the number of rows in a table.select count(*) from TABLE_NAME;Suppose we have established a connection with MySQL and created a table in the database named mydatabase using Statement object as://Creating the Statement object Statement stmt = con.createStatement(); //Query to create a table String query = "CREATE TABLE Cricketers_Data( "    + "First_Name VARCHAR(255), "    + "Last_Name VARCHAR(255), "    + "Date_Of_Birth Date, "    + "Place_Of_Birth VARCHAR(255), "    + "Country VARCHAR(255))"; //Executing the query stmt.execute(query); System.out.println("Table created......");In to this table we ... Read More

How to connect to Derby database using a JDBC program?

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

1K+ Views

Apache Derby is a Relational Database Management System which is fully based on (written/implemented in) Java programming language. It is an open source database developed by Apache Software Foundation.Installing derby:Follow the steps given below to install derby:Visit the home page of Apache Derby home page https://db.apache.org/derby/. Click the Download tab.Select and click on the link of the latest version of Apache Derby.On clicking the selected link, you will be redirected to the Distributions page of apache derby. If you observe here, derby provides distributions namely, db-derby-bin, db-derbylib.zip, db-derby-lib-debug.zip, and db-derby-src.zip.Download the db-derby-bin folder. Copy its contents to a separate folder ... Read More

How to connect to an SQLite database using a JDBC program?

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

1K+ Views

A. SQLite is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. It is a database, which is zero-configured, which means like other databases you do not need to configure it in your system.SQLite engine is not a standalone process like other databases, you can link it statically or dynamically as per your requirement with your application. SQLite accesses its storage files directly.The URL to connect with SQLite database is jdbc:sqlite:test.db and, the driver class name to connect to it is org.sqlite.JDBC.Before you proceed with the example:Download latest version of sqlite-jdbc-(VERSION).jar from sqlite-jdbcrepository.Add downloaded jar file ... Read More

How to call an existing function in a database using JDBC API?

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

6K+ Views

You can call a function using CallableStatement object just like stored procedures, to call a function using a JDBC program you need to.Connect to the database.Create a PreparedStatement object and to its constructor pass the function call in String format.Set values to the place holders.Execute the Callable statement.Following is the query to call a function from JDBC:{? = call getDob(?)}As you observe the query contains place holders (?) just like prepared and callable statements.In the above query, the first place holder represents the return value of the function and the second placeholder represents the input parameter.You need to register the ... Read More

How to create a function in a database using JDBC API?

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

960 Views

Like procedures, you can also create function in a database and store them.SyntaxFollowing is the syntax of creating a function in a(MySQL) database:CREATE FUNCTION Function_Name(input_arguments) RETURNS output_parameter BEGIN declare variables; statements . . . . . . . . . . return data_type; ENDTo create a function in a database using JDBC API you need to:Register the driver: class using the registerDriver() method of the DriverManager class. Pass the driver class name to it, as parameter.Establish a connection: Connect ot the database using the getConnection() method of the DriverManager class. Passing ... Read More

Advertisements