Java JDBC Tutorial  
          JDBC Tutorial Home
          Table of Contents
          Select Records
          Update Table
          Prepared Statements
          JDBC Result Sets
          Types of JDBC Drivers
          Create Table
          Insert Record
          Tutorials on Video
          Advertise
          Feedback
          Java Beginner
          Advanced Java Tutorials
          Java Interview Questions
          More Tech Tutorials


Java JDBC Tutorial

JDBC Create Table



Create SQL Table

Tables are composed of rows and columns. Each row represents a record in the database. Columns are also known as fields, or attributes. You can relate one database table to another by causing a given column in the table to derive its value from the value of a column in another table. If the tables had no columns in common, then there would be no way to relate them to one another. Because you can link tables together you can easily extract data from multiple tables with a single query, if your query mechanism supports this type of query.

In order to integrate your tables into a single database, you’ll need to ensure that each table has a column that contains a value unique to that table. Such a column is called a key. Below is a JDBC Program showing the use of executeUpdate() to create a table jdbc programming.

For my website I am creating the following 2 tables (Employee, Orders)

Employee_ID is the primary key which forms a relation between the 2 tables.

CREATE TABLE Employees (
Employee_ID INTEGER,
Name VARCHAR(30)
);

Employees:

Employee_ID Name
6323 Hemanth
5768 Bob
1234 Shawn
5678 Michaels

Orders:

CREATE TABLE Orders (
Prod_ID INTEGER,
ProductName VARCHAR(20),
Employee_ID INTEGER
);

Prod_ID Product Name Employee_ID
543 Belt 6323
432 Bottle 1234
876 Ring 5678
import javax.swing.JOptionPane;
import java.sql.*;
public class JDBCProgram{

	static String userid=”scott”, password = “tiger”;
	static String url = “jdbc:odbc:bob”;
	// String url = “jdbc:mySubprotocol:myDataSource”; ?
	static Statement stmt;
	static Connection con;
	public static void main(String args[]){

		JOptionPane.showMessageDialog
(null,”JDBC Programming showing Creation of Table’s”);
		int choice = -1;

		do{
			choice = getChoice();
			if (choice != 0){
				getSelected(choice);
			}
		}
		while ( choice !=  0);
			System.exit(0);
	}

	public static int getChoice()
	{
		String choice;
		int ch;
		choice = JOptionPane.showInputDialog(null,
			“1. Create Employees Table\n”+
			“2. Create Products Table\n”+
			“0. Exit\n\n”+
			“Enter your choice”);
		ch = Integer.parseInt(choice);
		return ch;

	}

	public static void getSelected(int choice){
		if(choice==1){
			createEmployees();
		}
		if(choice==2){
			createOrders();
		}
	}

	public static Connection getConnection()
	{

		try {
			Class.forName
(”sun.jdbc.odbc.JdbcOdbcDriver”);	//Class.forName(”myDriver.ClassName”); ?

		} catch(java.lang.ClassNotFoundException e) {
			System.err.print(”ClassNotFoundException: “);
			System.err.println(e.getMessage());
		}

		try {
			con = DriverManager.getConnection(url,
				 userid, password);

		} catch(SQLException ex) {
			System.err.println(”SQLException: ” + ex.getMessage());
		}

		return con;
	}

	/*CREATE TABLE Employees (
		    Employee_ID INTEGER,
		    Name VARCHAR(30)
		);*/
	public static void createEmployees()
	{
		Connection con = getConnection();

		String createString;
		createString = “create table Employees (” +
							“Employee_ID INTEGER, ” +
							“Name VARCHAR(30))”;
		try {
			stmt = con.createStatement();
	   		stmt.executeUpdate(createString);
			stmt.close();
			con.close();

		} catch(SQLException ex) {
			System.err.println(”SQLException: ” + ex.getMessage());
		}
		JOptionPane.showMessageDialog(null,”Employees Table Created”);
	}

	/*CREATE TABLE Orders (
		    Prod_ID INTEGER,
		    ProductName VARCHAR(20),
		    Employee_ID INTEGER
		);*/

	public static void createOrders()
	{
		Connection con = getConnection();

		String createString;
		createString = “create table Orders (” +
							“Prod_ID INTEGER, ” +
							“ProductName VARCHAR(20), “+
							“Employee_ID INTEGER )”;

		try {
			stmt = con.createStatement();
	   		stmt.executeUpdate(createString);

			stmt.close();
			con.close();

		} catch(SQLException ex) {
			System.err.println(”SQLException: ” + ex.getMessage());
		}
		JOptionPane.showMessageDialog(null,”Orders Table Created”);
	}

}//End of class

Download JDBC small



Technorati Tags: , ,

 
| | |
 
  Java is a trademark of Sun Microsystems, Inc. © Copyright 2007-08 jdbc-tutorial.com