Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jdbc Jdbc-mysql Inserting values in MySQL database table

Related Tutorials


 
 

Share on Google+Share on Google+

Inserting values in MySQL database table

Advertisement
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.

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
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 13, 2007

Related Tutorials

Discuss: Inserting values in MySQL database table   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:9
suraj kumar
April 14, 2011
insert statement with where statement

hi.. I m stopped here i cant find the write coding for the next step of my work. I will be thankful to if u send me the answer coding. i want to fill data into a predefined datatable by knowing the primary key of the particular column.
karthikeyan
May 31, 2011
mysql program

wow! what a simple and understandable coding
ashish
August 20, 2011
JDBC INSERT

import java.sql.*; public class dj { public static void main(String args[]) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con= DriverManager.getConnection("jdbc:odbc:ashish1","",""); Statement st=con.createStatement(); st.executeQuery("Select * from ashish1"); st.executeUpdate("INSERT ashish1 VALUES('nit',85)"); System.out.print("test"); ResultSet rs=st.getResultSet(); while(rs.next()) { System.out.print(rs.getString(1)); System.out.println(rs.getString(2)); //rs.executeUpdate(); } //con.close(); } catch(ClassNotFoundException e) { System.err.println(e); } catch(SQLException e) { System.err.println(e); } } } \\ Is this above code corretc how to insert the new data.
Venkat Santosh
November 23, 2011
i am getting an error

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
suresh
December 30, 2011
java

good
Ankit
January 14, 2012
issue with this code

This code does not close statement and connection . thats a serious resource leak.
allan
April 30, 2012
no comments

your code got no comments... It's hard to understand. Anyway, it's usable anyway.
blessy
September 17, 2012
error

when i execute it gives this error while executing java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268) at java.lang.ClassLoader.loadClass(ClassLoader.java:251) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:164) at insert.main(insert.java:12)
anon
October 9, 2012
i have a problem

sir, can you help me? when i execute insert commands my values in the textfield are not stored into the table. instead, it saves BLANK data. my program says it recorded the data but when i look at the table there is no data, only blank field.
DMCA.com