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).
#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
Hope It Will Help You :)
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.
-Create Class And Main Method.
-Register The Driver.
-Create The Connection.
-Create The Statement.
-Execute The Query.
-Create The Connection.
-Create The Statement.
-Execute The Query.
Hope It Will Help You :)
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
ReplyDeletehaving tried many casinos interesting online slots my choice was this site
ReplyDelete