Java Insert values in mysql table

Share:
JAVA INSERT VALUES IN MYSQL DATABASE TABLE USING A JAVA APPLICATION?
Hello Readers...

#Steps to insert values in MySql DataBase Table.
Create a database and a table with some columns according to your requirement.

#CREATE A DATABASE
To Create a DataBsae Execute The Following Statement.

create database your_database_name; //ex name mysql@123
#NOTE: create database is a sql command for creating a database.

#NOW CREATE A TABLE IN THAT DATABASE.
  CREATE TABLE IF NOT EXIST your_table_name(
 Fname varchar(20) NOT NULL,
 Lname varchar(20) NOT NULL
 );//ex table name tech-matin

  That's all for DataBase and Table Creation.

NOW CREATING A JAVA APPLICATION IN ECLIPSE.

To create a project in ECLIPSE go to file then click on new and select JAVA PROJECT,give a name to your project and finish creating your project.

Now before proceeding further make sure you have downloaded MySql Connector JAR File.

Now add that mysql connector jar file which you have downloaded to your project.

NOW CREATING A CLASS 
To create a class right click on src folder in your project and select class,provide package name and class name and finish creating a class(for example class name is MyClass).


import java.sql.*;
import java.io.*;
public class MyClass {

public static void main(String[] args){
try{
String driver,dusername,dpassword,url;
driver="com.mysql.jdbc.Driver";
dusername="root";//default for localhost
dpassword="";//blank for localhost
url="jdbc:mysql://localhost:3306/mysql@123";//url

Class.forName(driver);//Registering the driver
Connection conn=DriverManager.getConnection(url, dusername, dpassword);
System.out.println("Connected");//checking if connected

InputStreamReader myreader=new                                InputStreamReader(System.in);

BufferedReader read=new BufferedReader(myreader);
System.out.println("Enter Fname");
String Fname=read.readLine();
System.out.println("Enter Lname");
String Lname=read.readLine();

String query="INSERT INTO tech-matin values(?,?)";
PreparedStatement st=conn.prepareStatement(query);
st.setString(1, Fname);
st.setString(2, Lname);
st.executeUpdate();
System.out.println("Inserted");//checking if data inserted successfully
}//end of try
catch(Exception e){
System.out.println("Exception Occured "+e.getMessage());
}//end of catch
}//end of main
}//end of class

#Explanation:
Here In MyClass Class in main method we are performing insertion operation.

First we are creating connection with MySql Database.

To create a connection with MySql database we need to register the driver,url,username and password(See above example).

Now in the next step we are taking input from the user i.e., Fname and Lname using BufferedReader Class that extends Reader Class which take the object of InputStreamReader Class as argument.(See Above Example).

Now in the next step we are executing a String query to insert values in our database table using PreparedStatement.

At last executing the query using executeUpdate() method of PreparedStatement and printing Message if everything goes right.

NOW RUN THE PROGRAM ENTER Fname and Lname and press enter.

#NOW CHECK YOUR DATABASE TABLE IF VALUES INSERTED.

#QUICK REVISION:
#STEPS
-Create Project Add MySql Jar File.
-Create Class And Main Method.
-Register The Driver.
-Create The Connection.
-Create The Statement.
-Execute The Query.

Hope It Will Help You :)

3 comments:

  1. Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share. situs bokep

    ReplyDelete
  2. having tried many casinos interesting online slots my choice was this site

    ReplyDelete

You May Like Also

Programming Knowledge


Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database