Performing Database Operations in Java |SQL
INSERT,UPDATE,DELETE and SELECT.
This post is going to help you in learning how to do basic database operations using JDBC(Java Databse Connectivity) API.These basic operations are INSERT,UPDATE,SELECT and DELETE statements in SQL.Here we are going to use MySql DataBase.
Prerequisites:
-Eclipse IDE or Any
-JDK
-MySql Connector Jar File.
Creating Database:
-use the following statement
create database database_name;
Here i want my database name as thetechmatin;
so execute the following statement
create database thetechmatin;
That's all for Database Creation.
Now create a Table give name as users,execute the below statement.
create table users(Fname varchar(20),Lname varchar(20));
So now we have a database thetechmatin and a Table users with column Fname and Lname.
----------------------------
| Fname | Lname |
---------------------------
Now first we are going to perform INSERT Operation from java application.
-So a create project in Eclipse IDE,give any name for example JDBCOperations.
-Add MySql Connector Jar File.
-To add jar file to your project right click on project 1)now click on configure build path,2)click on Add External Jars,3)Nowfind your downloaded mysql connector jar file and double click on that jar file and say apply and close.
-Now Register the Driver Class.
To register the driver class use the below code
Class.forName("com.mysql.jdbc.Driver");
-Create the connection.
To create the connection with the Database use the below code
Use DriverManager Class Factory Method getConnection(); which returns the instance of Connection Interface.
Connection conn=DriverManager.getConnection("url","username","password");
-Create the statement.
-To create the statement use instance of Connection interface which you got in previous step,call createStatement() methos or PreparedStateemt() method using the conn instance.
conn.createStatement(); or conn.prepareStatement();//recommended
It will returns the instance of Statement OR PreparedStatement Interface,it will help us to execute the query using its factory methods i.e.,execute(),executeQuery(),executeUpdate() using these method we can get the instance of ResultSet Interface which will help us to get the data from the table.
See Below Example For Better UnderStanding
Now create a class give name as JDBCOperationsClass.
INSERT,UPDATE,DELETE and SELECT.
This post is going to help you in learning how to do basic database operations using JDBC(Java Databse Connectivity) API.These basic operations are INSERT,UPDATE,SELECT and DELETE statements in SQL.Here we are going to use MySql DataBase.
Prerequisites:
-Eclipse IDE or Any
-JDK
-MySql Connector Jar File.
Creating Database:
-use the following statement
create database database_name;
Here i want my database name as thetechmatin;
so execute the following statement
create database thetechmatin;
That's all for Database Creation.
Now create a Table give name as users,execute the below statement.
create table users(Fname varchar(20),Lname varchar(20));
So now we have a database thetechmatin and a Table users with column Fname and Lname.
----------------------------
| Fname | Lname |
---------------------------
Now first we are going to perform INSERT Operation from java application.
-So a create project in Eclipse IDE,give any name for example JDBCOperations.
-Add MySql Connector Jar File.
-To add jar file to your project right click on project 1)now click on configure build path,2)click on Add External Jars,3)Nowfind your downloaded mysql connector jar file and double click on that jar file and say apply and close.
-Now Register the Driver Class.
To register the driver class use the below code
Class.forName("com.mysql.jdbc.Driver");
-Create the connection.
To create the connection with the Database use the below code
Use DriverManager Class Factory Method getConnection(); which returns the instance of Connection Interface.
Connection conn=DriverManager.getConnection("url","username","password");
-Create the statement.
-To create the statement use instance of Connection interface which you got in previous step,call createStatement() methos or PreparedStateemt() method using the conn instance.
conn.createStatement(); or conn.prepareStatement();//recommended
It will returns the instance of Statement OR PreparedStatement Interface,it will help us to execute the query using its factory methods i.e.,execute(),executeQuery(),executeUpdate() using these method we can get the instance of ResultSet Interface which will help us to get the data from the table.
See Below Example For Better UnderStanding
Now create a class give name as JDBCOperationsClass.
- public class JDBCOperationsClass{
- public static void main(String args[]){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/tech matin","root","");
- String query="INSERT INTO users('matin','mansoori') VALUES(Fname,Lname)"; PreparedStatement ps=conn.prepareStatement(query);
- ps.executeUpdate();
- System.out.println("Inserted");
- ps.close();
- conn.close();
- }//end of try
- catch(Exception exx){
- System.out.print(exx.getMessage());
- }//end of catch
- }//end of main
- }//end of class
- public class JDBCOperationsClass{
- public static void main(String args[]){
- try{
- //registering the driver class
- Class.forName("com.mysql.jdbc.Driver");
- //creating connection
- Connection conn
- =DriverManager.getConnection("jdbc:mysql://localhost:3306/thetechmatin","root","");
- //writing query
- String query="Select DISTINCT(Fname) from users";
- PreparedStatement ps=conn.prepareStatement(query);
- ResultSet rs=ps.executeQuery();
- while(rs.next()){
- String name=rs.getString("Fname");
- System.out.println(name);
- }//end of while
- ps.close();
- conn.close();
- }//end of try
- catch(Exception exx){
- System.out.print(exx.getMessage());
- }//end of catch
- }//end of main
- }//end of class
- public class JDBCOperationsClass{
- public static void main(String args[]){
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn
- =DriverManager.getConnection("jdbc:mysql://localhost:3306/thetechmatin","root","");
- String query="UPDATE users set Fname='Dilshad' where Fname='matin' ";
- PreparedStatement ps=conn.prepareStatement(query);
- ps.executeUpdate();
- System.out.println("Updated");
- ps.close();
- conn.close();
- }//end of try
- catch(Exception exx){
- System.out.print(exx.getMessage());
- }//end of catch
- }//end of main
- }//end of class
- public class JDBCOperationsClass{
- public static void main(String args[]){
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn
- =DriverManager.getConnection("jdbc:mysql://localhost:3306/thetechmatin","root","");
- String query="DELETE * FROM users";
- PreparedStatement ps=conn.prepareStatement(query);
- ps.executeUpdate();
- System.out.println("Deleted");
- ps.close();
- conn.close();
- }//end of try
- catch(Exception exx){
- System.out.print(exx.getMessage());
- }//end of catch
- }//end of main
- }//end of class
column name must be fname, lname and values must be matin, mansoori
ReplyDeleteString query="INSERT INTO users('matin','mansoori') VALUES(Fname,Lname)";
it must have any conditions
DeleteDELETE * FROM users
thanks for making this blog.. u made easy to learn and understand .. thank you so much
ReplyDelete