Found 317 Articles for JDBC

How to remove a record from an existing table in oracle database using JDBC API?

Vikyath Ram
Updated on 30-Jul-2019 22:30:26

383 Views

You can remove a particular record from a table in a database using the DELETE query.SyntaxDELETE FROM table_name WHERE [condition];To delete a record from a table using JDBC API 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 to 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 Statement object using the createStatement() method of the Connection interface.Execute the Query: Execute the query using the executeUpdate() method of the ... Read More

How to retrieve a record from an existing table in oracle database using JDBC API?

Arushi
Updated on 30-Jul-2019 22:30:26

1K+ Views

You can update/modify the existing contents of a record in a table using the UPDATE query. Using this you can update all the records of the table or specific records.SyntaxUPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition];To update the contents of a record in a table using JDBC API 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 to the database using the getConnection() method of the DriverManager class. Passing URL (String), username (String), password (String) ... Read More

How to drop a table from Oracle database using JDBC API?

Arushi
Updated on 30-Jul-2019 22:30:26

174 Views

You can insert records into a table using the INSERT query.SyntaxINSERT INTO TABLE_NAME (column1, column2, column3, ...columnN) VALUES (value1, value2, value3, ...valueN); Or, INSERT INTO TABLE_NAME VALUES (value1, value2, value3, ...valueN);To insert a record into a table in a database using JDBC API 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 to 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 Statement object using the createStatement() ... Read More

How to create a table in Oracle using JDBC?

Vikyath Ram
Updated on 30-Jul-2019 22:30:26

2K+ Views

You can create a table in a database using the CREATE TABLE query.SyntaxCREATE TABLE table_name(    column1 datatype,    column2 datatype,    column3 datatype,    .....    columnN datatype,    PRIMARY KEY( one or more columns ) );To create a table in a database using JDBC API 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 Statement ... Read More

How to sql insert items from a list or collection in to table using JDBC?

Rishi Raj
Updated on 30-Jul-2019 22:30:26

5K+ Views

To insert the contents of a database to a collection, Connect to the database and retrieve the contents of the table into a ResultSet object using the SELECT Query.DriverManager.registerDriver(new com.mysql.jdbc.Driver()); String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from MyPlayers");Create a Java class to hold the contents of each record, with a variable and, setter and getter methods for each column (with suitable datatype).For example, if the table sample in the database has two fields with the details −Column name: ID, datatype: INT(11)Column name: Name, datatype VARCHAR(255)Then, the variable ... Read More

How to generate multiple insert queries via java?

Arushi
Updated on 30-Jul-2019 22:30:26

3K+ Views

JDBC provides a mechanism known as batch processing, in which you can group a set of INSERT or, UPDATE or, DELETE commands (those produce update count value) and execute them at once. You can insert multiple records in to a table using this.Adding statements to the batchStatement, PreparedStatement and CallableStatement objects hold a list (of commands) to which you can add related statements (those return update count value) using the addBatch() method.stmt.addBatch(insert1); stmt.addBatch(insert2); stmt.addBatch(insert3);Executing the batchAfter adding the required statements, you can execute a batch using the executeBatch() method of the Statement interface.stmt.executeBatch();Using batch updates, we can reduce the communication ... Read More

How to get Row and Column Count from ResultSet in JDBC

Arushi
Updated on 30-Jul-2019 22:30:26

2K+ Views

Row countThe last() method of the ResultSet interface moves the cursor to the last row of the ResultSet and, the getRow() method returns the index/position of the current row.Therefore, to get the number of rows move the cursor to the last row using the last() method and get the position of that (last) row using the getRow() method.Let 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) );Now, we will insert 7 records in ... Read More

How to get the row count from ResultSet in JDBC

Rishi Raj
Updated on 30-Jul-2019 22:30:26

7K+ Views

Whenever we execute SQL statements using the executeQuery() method, it returns a ResultSet object which holds the tabular data returned by the SELECT queries(in general). The ResultSet object contains a cursor/pointer which points to the current row. Initially this cursor is positioned before first row (default position).The ResultSet interface provides various methods to find, the no.of columns, name of the column, type of the column etc.. but, it does not provides any method to find the number of rows in a table directly.Using count(*) function in the SELECT query you can get the number of rows in a table as −select ... Read More

How to get column count in a ResultSet in JDBC?

Vikyath Ram
Updated on 30-Jul-2019 22:30:26

5K+ Views

You can get the column count in a table using the getColumnCount() method of the ResultSetMetaData interface. On invoking, this method returns an integer representing the number of columns in the table in the current ResultSet object.//Retrieving the ResultSetMetaData object ResultSetMetaData rsmd = rs.getMetaData(); //getting the column type int column_count = rsmd.getColumnCount();Let us create a table with name employee_data in MySQL database using CREATE statement as shown below −CREATE TABLE employee_data(    id INT,    Name VARCHAR(255),    DOB date,    Location VARCHAR(40) );Following JDBC program establishes connection with the database, retrieves the ResultSetMetaData object of the employee_data table, and prints the number of columns in it.Exampleimport ... Read More

How to get the table name of the current ResultSet using JDBC?

Arushi
Updated on 30-Jul-2019 22:30:26

2K+ Views

You can get the name of the table in the current ResultSet object using the getTableName() method of the ResultSetMetaData interface. This method accepts an integer value representing the index of a column and, returns a String value representing the name of the table that contains the given column.Let 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) );Now, we will insert 7 records in MyPlayers table using INSERT statements −insert into ... Read More

Previous 1 ... 4 5 6 7 8 ... 32 Next
Advertisements