0 votes
3k views
in JDBC by
Anyone can give jdbc crud example with description.

4 Answers

0 votes
by
edited

There are following code for inserting data into database:

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

public class InsertDataInToDatabase {

  /**
   @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub

    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=(ConnectionDriverManager.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.

by
reshown by
ít's  a connection to jdbc.How, if i want to connect SQL database
by
There are following code to connection sql database:

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class SqlServerConnectionExample{
 
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        String sqlServerDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        Class.forName(sqlServerDriver );   
        Connection conn = DriverManager.getConnection("jdbc:sqlserver://HOSP_SQL1.ad.uams.edu;user=root;password=w3c1re$h;database=root");
        System.out.println("Connection estiblished successfully!!");
        Statement sta = conn.createStatement();
        String Sql = "select * from employee";
        ResultSet rs = sta.executeQuery(Sql);
        while (rs.next()) {
            System.out.println(rs.getString("column_name"));
        }
    }
}
by
Sandeep gives an appropriate example for sql connection and insert data into sql.
by
I agree with Shalini answer.
0 votes
by

Following code for read data from database using JDBC API:

package com.developerhelpway.jdbc;

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

public class ReadDataFromDatabase {

    /**
     * @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 = "SELECT * FROM " + tableName;
          ResultSet resultSet = stat.executeQuery(sqlQuery);
          while(resultSet.next()){
              System.out.println("Id: " + resultSet.getInt("id") + " StudentName: " + resultSet.getString("studentName") + " Course: " + resultSet.getString("course") + " Roll:" + resultSet.getInt("roll") );
          }
        }
        catch(Exception e){
          e.printStackTrace();
        }
    }

}
 

Output:

Id: 1 StudentName: Vinod Course: MCA Roll:100

0 votes
by
Following code for updating the record into database:

package com.developerhelpway.jdbc;

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

public class UpdateDataInToDatabase {

    /**
     * @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 = "UPDATE " + tableName +" SET studentName = 'MBBS' WHERE id = 1";
          int num = stat.executeUpdate(sqlQuery);
          System.out.println(num + " rows updated.");
        }
        catch(Exception e){
          e.printStackTrace();
        }
    }

}
 

Output:

1 rows updated.
0 votes
by

Following code for deleting records from database:

package com.developerhelpway.jdbc;

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

public class DeleteDataInFromDatabase {

    /**
     * @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 = "DELETE FROM " + tableName +" WHERE id = 1";
          int num = stat.executeUpdate(sqlQuery);
          System.out.println(num + " rows deleted.");
        }
        catch(Exception e){
          e.printStackTrace();
        }
    }

}
 

Output:

1 rows deleted.

Share:- Whatsapp Facebook Facebook


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

Categories

...