0 votes
1.3k views
in JDBC by
How to insert data into database in java? Anyone can provide an appropriate example with code to insert data inot database.

1 Answer

0 votes
by

Following are the code for inserting data into database:

package com.developerhelpway.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class InsertDataInToDatabase {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String driverName="com.mysql.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/";
        String databaseName="jdbcexamples";
        String userName="root";
        String password="";
        String tableName = "student";
        Connection con = null;
        try{
          Class.forName(driverName);
          con=(Connection) DriverManager.getConnection(url+databaseName, userName, password);
          Statement stat=con.createStatement();
          String sqlQuery = "INSERT INTO student(studentName,course,roll) VALUES('Vinod','MCA',100)";
          int num = stat.executeUpdate(sqlQuery);
          System.out.println(num + " rows affected.");
        }
        catch(Exception e){
          e.printStackTrace();
        }
    }

}
 

Output:

1 rows affected.

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...