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


1
Expert's answer
2021-05-30T07:10:36-0400
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......... ……..");
   }
} 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS