JDBC Create Table Example Use Statement

This article contains java example about how to use java.sql.Statement to execute create mysql table sql scripts.

JDBC Statement Code Snippets

/* 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);
}

Complete Java Code

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();
			}
		}
	}

}

Execution Result

When the code execution complete, you can see that table ‘teacher’ has been created under the mysql test database. You can read article How To Use JDBC To Connect MySql Database┬áto learn more about MySql JDBC operation.

mysql table teahcer created in phpMyAdmin

Source Code

  1. Download “StatementCreateTableExample.zip” StatementCreateTableExample.zip – Downloaded 46 times – 1 KB

(Visited 137 times, 1 visits today)
READ :   JDBC CallableStatement Stored Procedure Input Parameters Example

Leave a Reply

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.