Create Table in JDBC

Create Table in JDBC

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

Java File Name - CreatingTable.java

package com.jdbc;

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

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

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

public class CreatingTable {

/**
* @param args
* @throws ClassNotFoundException 
* @throws SQLException 
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection con = null;
java.sql.PreparedStatement stmt= null;

Class.forName("com.mysql.jdbc.Driver");
try {
con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/learnjava","root","ramu");
String sql = "CREATE TABLE VIDEO_TABLE"+
"(ID INTEGER NOT NULL,"+
" FIRST_NAME VARCHAR(10),"+
"LAST_NAME VARCHAR(10),"+
"PRIMARY KEY (ID))";


stmt = con.prepareStatement(sql);
stmt.executeUpdate();


} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(con != null){
System.out.println("Table has been created");
con.close();
}
}
}

}



0 comments:

Post a Comment