JDBC Create Table Example Use Statement

This article contains a java example of how to use the java.sql.Statement to execute create MySQL table SQL scripts.

1. JDBC Statement Code Snippets

  1. The below java snippet source code shows how to create the java.sql.Connection object and create the java.sql.Statement object and run a SQL script through the java.sql.Statement object.
    /* Get mysql connection object.*/
    java.sql.Connection conn = this.getMySqlConnection(ip, port, dbName, userName, password);
    if(conn!=null)
    {
    	/* Create Statement object. */
            java.sql.Statement stmt = conn.createStatement();
    				
    	/* Execute create table sql command. */
    	stmt.execute(createTableSql);
    }
    

2. Complete Java Code

  1. StatementCreateTableExample.java
    public class StatementCreateTableExample {
    
    	public static void main(String[] args) {
    		
    		StatementCreateTableExample scte = new StatementCreateTableExample();
    		
    		/* This is the sql command to create mysql table. */
    		String createMySqlTableSql = "CREATE TABLE `test`.`teacher` ( `name` VARCHAR(100) NOT NULL , `email` VARCHAR(100) NOT NULL ) ENGINE = InnoDB; ";
    	
    		
    		scte.createMySQLTable("localhost", 3306, "test", "root", "", createMySqlTableSql);
    	}
    	
    	/* This method return java.sql.Connection object from MySQL server. */
    	public Connection getMySqlConnection(String ip, int port, String dbName, String userName, String password)
    	{
    		/* Declare and initialize a sql Connection variable. */
    		Connection ret = null;
    		
    		try
    		{
    		
    			/* Register for mysql jdbc driver class. */
    			Class.forName("com.mysql.jdbc.Driver");
    			
    			/* Create mysql connection url. */
    			String mysqlConnUrl = "jdbc:mysql://" + ip + ":" + port + "/" + dbName;
    			
    			/* Get the mysql Connection object. */
    			ret = DriverManager.getConnection(mysqlConnUrl, userName , password);
    		}catch(Exception ex)
    		{
    			ex.printStackTrace();
    		}finally
    		{
    			return ret;
    		}
    	}
    	
    	public void createMySQLTable(String ip, int port, String dbName, String userName, String password, String createTableSql)
    	{
    		Connection conn = null;
    		Statement stmt = null;
    		try
    		{
    			/* Get mysql connection object.*/
    			conn = this.getMySqlConnection(ip, port, dbName, userName, password);
    			if(conn!=null)
    			{
    				/* Create Statement object. */
    				stmt = conn.createStatement();
    				
    				/* Execute create table sql command. */
    				stmt.execute(createTableSql);
    			}
    			
    		}catch(Exception ex)
    		{
    			ex.printStackTrace();
    		}finally
    		{
    			/* Release statment and connection object. This can save system resources. */
    			try
    			{
    				if(stmt!=null)
    				{
    					stmt.close();
    					stmt = null;
    				}
    				
    				if(conn!=null)
    				{
    					conn.close();
    					conn = null;
    				}
    				
    			}catch(Exception ex)
    			{
    				ex.printStackTrace();
    			}
    		}
    	}
    
    }
    

3. Execution Result.

  1. When the code execution is complete, you can see that the table teacher has been created under the MySQL test database.
  2. You can read the article How To Use JDBC To Connect MySql Database to learn more about MySQL JDBC operation.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Index