QN043. Write a program to insert record into database table.
1. Objective
To insert record into databse table.
2. Theory
In this program, we create statement and execute query to insert record data into database.
3. Source Code
import java.sql.*;
class JDBC43{
public static void main(String[] args){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/bca_third_2020?serverTimezone=UTC";
String un = "root";
String pw = "";
Connection cn = DriverManager.getConnection(url,un,pw);
insert(cn);
}catch(Exception e){
System.out.println(e);
}
}
private static void insert(Connection cn) throws SQLException {
Statement stat = cn.createStatement(); // statement create
String query = "insert into student (roll_no, name, course) values (5, 'Sangina', 'BIM')";
stat.execute(query); // execute query
System.out.println("Record inserted!");
}
}
4. Output
Record inserted!
5. Conclusion
In this way, I would like to conclude that, we can insert into database table using this progam.