Question #200395

Mysql database connectivity through Java program

insert data using Java program fields are given below for insert

student name, student father name, student mother name, student roll number, student address


Expert's answer

import java.sql.DriverManager;
import java.sql.*;
import java.sql.SQLException;

public class StudentInfo {
   public static void main(String[] args) {
      Connection conn = null;
      Statement stmt = null;
      try {
         try {
            Class.forName("com.mysql.jdbc.Driver");
         } catch (Exception e) {
            System.out.println(e);
      }
      conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/university", "root", "12345");
      System.out.println("Connection is created successfully:");
      stmt = (Statement) conn.createStatement();
      String query1 = "INSERT INTO `university`.`student`"
      +"(`roll_no`,`stu_name`,`stu_fname`,`stu_mname`,'stu_address')VALUES"
      +"('IT01','xyz','KLM','abc','x a d v')";
            
        
      stmt.executeUpdate(query1);
      
      System.out.println("Record is inserted in the table successfully..................");
      } catch (SQLException excep) {
         excep.printStackTrace();
      } catch (Exception excep) {
         excep.printStackTrace();
      } finally {
         try {
            if (stmt != null)
               conn.close();
         } catch (SQLException se) {}
         try {
            if (conn != null)
               conn.close();
         } catch (SQLException se) {
            se.printStackTrace();
         }  
      }
      System.out.println("Please check it in the MySQL Table......... ……..");
   }
} 
LATEST TUTORIALS
APPROVED BY CLIENTS