Inserting values in MySQL database table

After making the table in the database, we need to
insert the values in the database. Here we are going to see, how we can insert values in
the MySQL database table. We know that tables store data in rows and
column format. After creating a database table, you need to insert the values in it. In
this section, we are providing an example with code that provides the facility
for inserting the values in MySQL database table.
Description of program:
First of all this program establishes the connection
with MySQL database through the JDBC driver, after only that we will be
able to insert the values in
specific table with the help of some APIs and methods. If any values get
inserted in the table then shows a message "1 row affected" but
if any problems comes while inserting the data in the table then it will displays
the message "SQL statement
is not executed!".
Description of code:
INSERT table_name VALUES(field_values):
Above code is used, when you want to insert values in the database table
with appropriate value.
Here is the code of program:
import java.sql.*;
public class InsertValues{
public static void main(String[] args) {
System.out.println("Inserting values in Mysql database table!");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","root");
try{
Statement st = con.createStatement();
int val = st.executeUpdate("INSERT employee VALUES("+13+","+"'Aman'"+")");
System.out.println("1 row affected");
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
|
Download this example.
Output of program:
C:\vinod\jdbc\jdbc\jdbc-mysql>javac
InsertValues.java
C:\vinod\jdbc\jdbc\jdbc-mysql>java InsertValues
Inserting values in Mysql database table!
1 row affected |

|
Current Comments
3 comments so far (post your own) View All Comments Latest 10 Comments:Sir can we know the exact code of
Inserting a record on Employees Table?
Thank you
Posted by rey on Wednesday, 12.19.07 @ 13:31pm | #42940
This is my script exactly how you done it but i've got one error:"The statement SQL isn't executed General error";WHY?
import java.sql.*;
class JDBCex
{
public static void main(String[] args)
{
System.out.println("Inserting values in MS Access database table!");
Connection conn = null;
String url = "jdbc:odbc:formular";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
try
{
Class.forName(driver);
conn = DriverManager.getConnection(url,"","");
try
{
Statement stmt = conn.createStatement();
int val = stmt.executeUpdate("INSERT INTO angajati VALUES("+13+","+"'ion'"+","+"'ion'"+","+"'ion'"+" )");
System.out.println("1 row affected");
}
catch(SQLException e)
{
System.out.println("SQL Statement isn't executed " + e.getMessage());
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Posted by ion on Tuesday, 07.31.07 @ 15:19pm | #22302
Hi
In this example some mistake is there in sql command
Insert into tablename values() but it is Insert tablename values()..
so please correct it.
with regrds,
pavan kumar chelluri
Posted by pavan kumar chelluri on Sunday, 07.8.07 @ 10:52am | #20907