Found 6702 Articles for Database

Is it mandatory to close JDBC connections?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

2K+ Views

At the end of your JDBC program, it is required explicitly to close all the connections to the database to end each database session. However, if you forget, Java's garbage collector will close the connection when it cleans up stale objects.Relying on the garbage collection, especially in database programming, is a very poor programming practice. You should make a habit of always closing the connection with the close() method associated with connection object.To ensure that a connection is closed, you could provide a 'finally' block in your code. A finally block always executes, regardless of an exception occurs or not.To ... Read More

How to call a stored procedure using callable statement in JDBC explain?

Naveen Singh
Updated on 09-Mar-2020 06:37:08

4K+ Views

You can call the SQL stored procedures using the CallableStatement interface. A Callable statement can have input parameters, output parameters, or both.You can create an object of the CallableStatement (interface) using the prepareCall() method of the Connection interface. This method accepts a string variable representing a query to call the stored procedures and returns a CallableStatement object.Suppose you have a procedure name myProcedure in the database you can prepare a callable statement as://Preparing a CallableStatement CallableStatement cstmt = con.prepareCall("{call myProcedure(?, ?, ?)}");Then you can set values to the place holders using the setter methods of the CallableStatement interface and execute ... Read More

Among all 4 JDBC driver types, when to use which driver?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

119 Views

If you are accessing one type of database, such as Oracle, Sybase, or IBM, the preferred driver type is 4.If your Java application is accessing multiple types of databases at the same time, type 3 is the preferred driver.Type 2 drivers are useful in situations, where a type 3 or type 4 driver is not available yet for your database.The type 1 driver is not considered a deployment-level driver, and is typically used for development and testing purposes only.

What is the difference between execute(), executeQuery() and executeUpdate() methods in JDBC?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

18K+ Views

Once you have created the statement object you can execute it using one of the execute methods of the Statement interface namely, execute(), executeUpdate() and, executeQuery().The execute() method: This method is used to execute SQL DDL statements, it returns a boolean value specifying weather the ResultSet object can be retrieved.Exampleimport java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Example {    public static void main(String args[]) throws SQLException {       //Registering the Driver       DriverManager.registerDriver(new com.mysql.jdbc.Driver());       //Getting the connection       String mysqlUrl = "jdbc:mysql://localhost/sampleDB";       Connection con = ... Read More

Count multiple occurrences of separate texts in MySQL?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

273 Views

You can use aggregate function count along with if() for this. To understand the concept, let us create a table. The query to create a table is as followsmysql> create table CountOccurrencesDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> TechnicalSubject varchar(100)    -> ); Query OK, 0 rows affected (0.68 sec)Now you can insert some records in the table using insert command. The query is as followsmysql> insert into CountOccurrencesDemo(TechnicalSubject) values('Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into CountOccurrencesDemo(TechnicalSubject) values('MongoDB'); Query OK, 1 row affected (0.13 sec) mysql> insert into ... Read More

How to select only non - numeric values from varchar column in MySQL?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

970 Views

You need to use REGEXP for this. The syntax is as followsSELECT *FROM yourTableName WHERE yourColumnName REGEXP '[a-zA-Z]';To understand the concept, let us create a table. The query to create a table is as followsmysql> create table SelectNonNumericValue    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserId varchar(100)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command. The query is as followsmysql> insert into SelectNonNumericValue(UserId) values('123John'); Query OK, 1 row affected (0.12 sec) mysql> insert into SelectNonNumericValue(UserId) values('58475Carol98457Taylor24'); Query OK, 1 row affected (0.52 sec) ... Read More

Create a new user with password in MySQL 8?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

3K+ Views

You need to use CREATE command to create a new user with password in MySQL 8. Let us check the versionmysql> select version(); +-----------+ | version() | +-----------+ | 8.0.12    | +-----------+ 1 row in set (0.14 sec)The syntax is as follows to create a new user with passwordCREATE USER 'yourUserName'@'localhost' IDENTIFIED BY 'yourPassword';The following is the syntax to grant all privileges to the created userGRANT ALL ON *.* TO 'yourUserName'@'localhost';Now flush the privileges using flush commandflush privileges; Let us create a new user with the help of the above syntax. The query is as followsmysql> use MySQL; Database ... Read More

MySQL Select Statement DISTINCT for Multiple Columns?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

479 Views

To understand the MySQL select statement DISTINCT for multiple columns, let us see an example and create a table. The query to create a table is as followsmysql> create table selectDistinctDemo    -> (    -> InstructorId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentId int,    -> TechnicalSubject varchar(100)    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command. The query is as followsmysql> insert into selectDistinctDemo(StudentId, TechnicalSubject) values(121, 'Java'); Query OK, 1 row affected (0.15 sec) mysql> insert into selectDistinctDemo(StudentId, TechnicalSubject) values(121, 'MongoDB'); Query OK, 1 row affected (0.16 ... Read More

How to move data between two tables with columns in different MySQL databases?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

502 Views

For this, you need to use an INSERT SELECT statement. The syntax is as followsINSERT INTO yourDatabaseName1.yourTableName1(yourColumnName1, yourColumnName2, ....N) SELECT yourColumnName1, yourColumnName2, ....N FROM yourdatabaseName2.yourTableName2;Here, I am using the following two databasessampletestLet us create the first table in the “test” databasemysql> use test; Database changed mysql> create table send    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20) -> ); Query OK, 0 rows affected (1.19 sec)Insert some records in the first table using insert command. The query is as followsmysql> insert into send(Name) values('John'); Query OK, 1 row affected ... Read More

How to select first and last row record using LIMIT in MySQL?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

1K+ Views

Let us first create a table. The query to create a table is as followsmysql> create table FirstAndLastDataDemo    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> EmployeeName varchar(20),    -> EmployeeAge int    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command. The query is as followsmysql> insert into FirstAndLastDataDemo(EmployeeName, EmployeeAge) values('John', 23); Query OK, 1 row affected (0.15 sec) mysql> insert into FirstAndLastDataDemo(EmployeeName, EmployeeAge) values('Bob', 13); Query OK, 1 row affected (0.11 sec) mysql> insert into FirstAndLastDataDemo(EmployeeName, EmployeeAge) values('Larry', 24); Query OK, 1 row affected ... Read More

Advertisements