The post (“JDBC Create Database”) will give an example, and show how to create a database using JDBC.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package net.tecbar.jdbc.examples; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcCreateDatabaseExample { private final static String USERNAME = "deploy"; private final static String PASSWORD = "Ts78W$k"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ // Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); // Open a connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/", USERNAME, PASSWORD); // Execute a query stmt = conn.createStatement(); final String sql = "CREATE DATABASE mytecbar"; stmt.executeUpdate(sql); } catch(SQLException sle){ // process exception here } catch(Exception e){ // process exception here } finally{ release(conn, stmt, null); } } public static void release(Connection conn, Statement stmt, ResultSet rs) { try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException ex) { // process exception here } } } |
Pingback: JDBC by Examples