Sunday, 17 July 2016

JDBC PreparedStatement – Create a table

package net.dharmaraj;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JDBCPreparedStatementCreateExample {
//============================================================
public static void main(String[] args) {
try {
createTable();
} catch (SQLException e) {
System.out.println(e.getMessage());
}//end of try catch block
}//end of main method
//============================================================
private static void createTable() throws SQLException {

Connection dbConnection = null;
PreparedStatement preparedStatement = null;

String createTableSQL = "CREATE TABLE DBUSER1("
+ "USER_ID INT(5) NOT NULL, "
+ "USERNAME VARCHAR(20) NOT NULL, "
+ "CREATED_BY VARCHAR(20) NOT NULL, "
+ "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) "
+ ")";

try {
dbConnection = JDBCStatementCreateExample.getDBConnection();
preparedStatement = dbConnection.prepareStatement(createTableSQL);
System.out.println(createTableSQL);
preparedStatement.executeUpdate();
System.out.println("Table \"dbuser\" is created!");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}//end of try catch block
}//end of Method
//============================================================
}//END OF Class

No comments:

Post a Comment