Data Insertion into DB by using JDBC



Required Jar files
                 1)   Mysql-connector.jar(for connecting MYSQL DB)
                 2)      Ojdbc14.jar(for Oracle DB)

Java File Name - DBDataInsertion.java

package com.jdbc;

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

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

/**
 * @Author -- Ramu Chintha
 */

public class DBDataInsertion {

/**
* @param args
* @throws ClassNotFoundException 
* @throws SQLException 
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
Connection con = null;
PreparedStatement pstmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/learnjava","root","ramu");
String sql ="INSERT INTO VIDEO_TABLE VALUES(1,'TEST','LASTNAME')";
pstmt= (PreparedStatement) con.prepareStatement(sql);
pstmt.executeUpdate();
System.out.println("Data inserted into table");
}
catch(Exception e){
e.printStackTrace();

}
finally{
// Here we will close the DB Conneciton
System.out.println("Connection is closing");
if(con != null){
con.close();
}
}

}

}





0 comments:

Post a Comment