Create Table


Leave a reply                     

Create Table in Java

MySql Create Table

Java Create Table

How to Create Table In MySql in Java?

Following program creates table tablename in MySql database databasename.
columnname1 and columnname2 are name of columns in table.
auto_increment is used to make column auto increment.
primary key makes columnname1 primary key.

package raghuwansh.java.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateTable {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/databasename", "root", "");
			Statement statement = connection.createStatement();
			statement
					.executeUpdate("CREATE TABLE tablename(columnname1 int(5) auto_increment primary key, columnname2 varchar(128))");
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

Leave a Reply

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


+ 2 = eleven