Execute SQL in Java

Price: INR 8,999.00
Price: INR 15,999.00
Was: INR 16,999.00

In the previous tutorial, We have seen the use of preparedStatement in JDBC. In this tutorial you will see, How can you execute SQL in Java using JDBC. We have also see the one example of DDL (Select query) in the previous tutorial.

 

In this tutorial we will see, How can we perform the DML using JDBC. It’s important to know these concepts, In selenium nothing is ready-made, you have to work hard and prepare your own scripts as per the requirements. So it’s important to get an idea about the some JDBC as well.

In our example, we are taking the ‘Insert’ query example.

 

Let’s open your MySQL database and see the structure of the table in which you want to perform the insertion.

In our example, We have created the new Database named ‘Seleniumbix’ in our DB and created the new table ’emp’ in seleniumbix DB. Now let’s see the structure of emp table –

DDL DCL Statements in MySQL

 

You can see, We have the table named ’emp’ in our database and here we have 4 columns. In this table first column, ’empid’ is auto incremented. So in insertion, we have to insert only 3 columns.

Suppose our insertion query is –

insert into emp(emp_name, emp_firm, emp_country)

            values (‘David Sausa’, ‘Siemens’, ‘Germany’);

 

Now we will perform this insertion using Java Application. Let’s see, How can we do this-

 

By default connection is auto-commit, so all insert queries will be committed automatically by Java connection.

Now let’s see the result of above code in our DB.

DML & DDL Satements in Java

 

You can see, Our query was inserted successfully. you can use the above code in your local. You need to make some minor modification like DB name and SQL query.

 

So that’s all, How can we perform the DML using JDBC. We will see these all concepts in details in Java JDBC tutorials.

0 Comment

Leave a Reply