JDBC Articles

Page 14 of 22

What is the MySQL datatype to store DATALINK object in JDBC

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 482 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..MySQL does not provide any separate datatype to store DATALINK/URL value you need to store using TEXT or VARCHAR datatypes as shown in the following query −CREATE TABLE tutorials_data (    tutorial_id INT PRIMARY KEY AUTO_INCREMENT,    tutorial_title VARCHAR(100),    tutorial_author VARCHAR(40),    submission_date date,    tutorial_link VARCHAR(255) );Following JDBC program establishes a connection with MYSQL database, creates a table with name tutorials_data. In this table we are creating a column with name tutorial_link which stores ...

Read More

How to handle indexes in JavaDB using JDBC program?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 1K+ Views

Indexes in a table are pointers to the data, these speed up the data retrieval from a table. If we use indexes, the INSERT and UPDATE statements get executed in a slower phase. Whereas SELECT and WHERE get executed with in lesser time.Creating an indexCTREATE INDEX index_name on table_name (column_name);Displaying the IndexesSHOW INDEXES FROM table_name;Dropping an indexDROP INDEX index_name;Following JDBC program creates a table with name Emp in JavaDB. creates an index on it, displays the list of indexes and, deletes the created index.Exampleimport java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class IndexesExample {    public static void main(String ...

Read More

How to set the initial value of an auto-incremented column in MySQL using JDBC?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 2K+ Views

While creating a table, in certain scenarios, we need values to column such as ID, to be generated/incremented automatically. Various databases support this feature in different ways.In MySQL database you can declare a column auto increment using the following syntax.CREATE TABLE table_name(    ID INT PRIMARY KEY AUTO_INCREMENT,    column_name1 data_type1,    column_name2 data_type2,    column_name3 data_type3,    column_name4 data_type4,    ............ ........... );While inserting records in a table there is no need to insert value under the auto-incremented column. These will be generated automatically.Setting the initial valueBy default, the initial value of the auto-incremented column will be 1. You ...

Read More

How to get primary key value (auto-generated keys) from inserted queries using JDBC?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 5K+ Views

If you insert records into a table which contains auto-incremented column, using a Statement or, PreparedStatement objects.You can retrieve the values of that particular column, generated by them object using the getGeneratedKeys() method.ExampleLet us create a table with name sales in MySQL database, with one of the columns as auto-incremented, using CREATE statement as shown below −CREATE TABLE Sales(    ID INT PRIMARY KEY AUTO_INCREMENT,    ProductName VARCHAR (20),    CustomerName VARCHAR (20),    DispatchDate date,    DeliveryTime time,    Price INT,    Location VARCHAR(20) );Retrieving auto-generated values (PreparedStatement object)Following JDBC program inserts 3 records into the Sales table (created above) ...

Read More

How to set values to list of parameters of IN clause on PreparedStatement using JDBC?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 4K+ Views

The IN clause in MYSQL database is used to specify the list of parameters in a query.For example, you need to retrieve contents of a table using specific IDs you can do so using the SELECT statement along with the IN clause as −mysql> SELECT * from sales where ID IN (1001, 1003, 1005); +------+-------------+--------------+--------------+--------------+-------+------------+ | ID   | ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location | +------+-------------+--------------+--------------+--------------+-------+------------+ | 1001 | Key-Board   | Raja         | 2019-09-01   | 11:00:00 | 8500 | Hyderabad ...

Read More

Is it Possible to store and retrieve boolean values in a VARCHAR2 column in a table using JDBC?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 679 Views

Yes, in Oracle you can store and retrieve a boolean value into a table for a column with VARCHAR2 datatype.If you do so, the true and false values are stored as 1 and 0 and the retrieved as same (respectively).ExampleLet us create a table with name sampleTable in the Oracle database Using CREATE statement as −CREATE TABLE sampleTable(    ID INT,    ProductName VARCHAR (20) NOT NULL,    CustomerName VARCHAR (20) NOT NULL,    IsBillDue VARCHAR (20) NOT NULL,    DeliveryDate date,    Price INT,    Location varchar(20) );The column IsBillDue specified whether bill paid.Following JDBC program establishes the connection ...

Read More

How to get the data type name from the java.sql.Type code using JDBC?

Smita Kapse
Smita Kapse
Updated on 30-Jul-2019 856 Views

The java.sql.Types class represents the SQL datatype in integer format. The valueOf() method of the enumeration JDBCType accepts an integer value representing the java.sql.Type and, returns the JDBC type corresponding to the specified value.ExampleLet 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) );Following JDBC program establishes connection with the MySQL database retrieves the contents of the MyPlayers table into a ResultSet object, obtains its metadata, obtains the column ...

Read More

How to create a table with decimal values using JDBC?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 852 Views

An unpacked floating-point number that cannot be unsigned. In the unpacked decimals, each decimal corresponds to one byte. Defining the display length (M) and the number of decimals (D) is required. NUMERIC is a synonym for DECIMAL.To define a column with a decimal value as datatype follow the syntax given below −column_name DECIMAL(P, D);Where −P is the precision representing the number of digits (range 1 to 65)D is the scale representing the number of digits after the decimal point.Note − in MySQL D should be describe customers; +------------+---------------+------+-----+---------+-------+ | Field      | Type          | ...

Read More

How to store decimal values in a table using PreparedStatement in JDBC?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 1K+ Views

To insert records into a table that contains a decimal value using PreparedStatement you need to −Register the driver − 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 URL (String), username (String), password (String) as parameters to it.Create Statement − Create a PreparedStatement object using the prepareStatement() method of the Connection interface. Pass the INSERT query with place holders to this method in String format as a parameter.PreparedStatement pstmt = con.prepareStatement("INSERT ...

Read More

Batch Inserts Using JDBC Prepared Statements

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 6K+ Views

Grouping a set of INSERT Statements and executing them at once is known as batch insert.Batch inserts using PreparedStatement objectTo execute a batch of insert statements using the PreparedStatement object −Create a PreparedStatement − Create a PreparedStatement object using the prepareStatement() method. Pass the Insert query with place holders “?” instead of values as a parameter to this method.PreparedStatement pstmt = con.prepareStatement("INSERT INTO Sales VALUES (?, ?, ?, ?, ?)");Set the values to the place holders − Using the setXXX() methods (setInt(). SetString(), setFloat() etc…) set the values to the place holders in the PrepareStatement as −pstmt.setString(1, "KeyBoard"); pstmt.setString(2, "Amith"); ...

Read More
Showing 131–140 of 220 articles
« Prev 1 12 13 14 15 16 22 Next »
Advertisements