Here’s an example to show you how to create a table in database via JDBC statement. To issue a create statement, calls the S
tatement.execute()
method like this :Statement statement = dbConnection.createStatement(); // execute create SQL stetement statement.execute(createTableSQL);
Full example…
package com.mkyong.jdbc; import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; public class JDBCStatementCreateExample { private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver"; private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:MKYONG"; private static final String DB_USER = "user"; private static final String DB_PASSWORD = "password"; public static void main(String[] argv) { try { createDbUserTable(); } catch (SQLException e) { System.out.println(e.getMessage()); } } private static void createDbUserTable() throws SQLException { Connection dbConnection = null; Statement statement = null; String createTableSQL = "CREATE TABLE DBUSER(" + "USER_ID NUMBER(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 = getDBConnection(); statement = dbConnection.createStatement(); System.out.println(createTableSQL); // execute the SQL stetement statement.execute(createTableSQL); System.out.println("Table \"dbuser\" is created!"); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { if (statement != null) { statement.close(); } if (dbConnection != null) { dbConnection.close(); } } } private static Connection getDBConnection() { Connection dbConnection = null; try { Class.forName(DB_DRIVER); } catch (ClassNotFoundException e) { System.out.println(e.getMessage()); } try { dbConnection = DriverManager.getConnection( DB_CONNECTION, DB_USER,DB_PASSWORD); return dbConnection; } catch (SQLException e) { System.out.println(e.getMessage()); } return dbConnection; } }
Result
Here’s the result.
CREATE TABLE DBUSER( USER_ID NUMBER(5) NOT NULL, USERNAME VARCHAR(20) NOT NULL, CREATED_BY VARCHAR(20) NOT NULL, CREATED_DATE DATE NOT NULL, PRIMARY KEY (USER_ID) ) TABLE "user" IS created!
Good...
ReplyDeletethanq
Deletepackers and movers noida sector 12 @ http://www.noidapackers.co.in/
ReplyDeletepackers and movers noida sector 53 @ http://www.noidapackers.co.in/
packers and movers noida sector 51 @ http://www.noidapackers.co.in/
packers and movers noida sector 50 @ http://www.noidapackers.co.in/
packers and movers noida sector 49 @ http://www.noidapackers.co.in/
packers and movers noida sector 41 @ http://www.noidapackers.co.in/
packers and movers noida sector 39 @ http://www.noidapackers.co.in/
packers and movers noida sector 62 @ http://www.noidapackers.co.in/
packers and movers noida sector 61 @ http://www.noidapackers.co.in/
jianbin1205
ReplyDeletecheap uggs
north face outlet online
coach outlet online
uggs outlet
snapback hats
true religion outlet,true religion jeans,true religion kids,true religion jeans sale,true religion jeans for men,true religion jacket,true religion sale
cheap oakley sunglasses
moncler coats
ugg boots on sale
cyber monday
kobe bryants shoes 2015
polo ralph lauren uk
moncler outlet
lululemon outlet
prada outlet
chanel handbags
ugg uk outlet
toms outlet
canada goose outlet
the north face jackets
new england patriots
chicago blackhawks
michael kors outlet sale
new york giants
air force one shoes
nike air max 90
ugg boots
air jordan 11 free shipping
michael kors handbags
nike trainers uk
air jordan shoes
kansas city chiefs
chicago bulls
We are offering website service or website design for your company and much more...........
ReplyDeleteBuzz Apps
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteBest BCA Colleges in Noida