Welcome to Dream.In.Code
Become a Java Expert!

Join 149,824 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,610 people online right now. Registration is fast and FREE... Join Now!




creating GUI for creating tables

 
Reply to this topicStart new topic

creating GUI for creating tables

cypriot
16 Feb, 2007 - 02:55 PM
Post #1

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 2


My Contributions
Hi. I am creating a GUI which involves creating,deleting and selecting tables and records. I am confused about how I should arrange classes.I need to create four tables and each should have different GUI.



import java.sql.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class modifyTables {

private static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
private static final String BASE_URL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\projec\\proj-1.mdb";


public static void addAccommodatiom(String Acc_name, String Acc_type,String Acc_address, String Acc_Location, Long Acc_Phoneno, Long Acc_Faxno,
String dis_sea,String dis_airport) throws SQLException {

final String SELECT_KEY_SQL = "select @@id_accomm";
final String sql = "INSERT INTO Accommodation VALUES (?,?,?,?,?,?,?,?)";

try{


Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement pstmt = databaseConnection.prepareStatement(sql);
pstmt.setString(1, Acc_name);
pstmt.setString(2, Acc_type);
pstmt.setString(3, Acc_address);
pstmt.setString(4, Acc_Location);
pstmt.setLong(5, Acc_Phoneno);
pstmt.setLong(6, Acc_Faxno);
pstmt.setString(7, dis_sea);
pstmt.setString(8,dis_airport);
pstmt.executeUpdate();
pstmt.close();

}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void addRestaurant (String Res_name, String Res_type,String Res_address, String Res_location,
Long Res_Phoneno) throws SQLException {
final String sql = "INSERT INTO Restaurants VALUES (?,?,?,?,?,?)";
final String SELECT_KEY_SQL = "select @@id_accomm";
try {
Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement pstmt = databaseConnection.prepareStatement(sql);
pstmt.setString(1, Res_name);
pstmt.setString(2,Res_type);
pstmt.setString(3,Res_address);
pstmt.setString(4,Res_location);
pstmt.setLong(5,Res_Phoneno);
pstmt.executeUpdate();
pstmt.close();

} catch (Exception e)
{
e.printStackTrace();
}

}
public static void removeRestaurant(String Res_name, String Res_type,String Res_address, String Res_location,
Long Res_Phoneno) throws SQLException {
final String sql= "DELETE INTO Restaurants VALUES(?,?,?,?,?)";
final String SELECT_KEY_SQL = "select @@id_accomm";
try{
Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement pstmt = databaseConnection.prepareStatement(sql);
pstmt.setString(1, Res_name);
pstmt.setString(2,Res_type);
pstmt.setString(3,Res_address);
pstmt.setString(4,Res_location);
pstmt.setLong(5,Res_Phoneno);
pstmt.close();
} catch (Exception e)
{
e.printStackTrace();
}

}
public static void removeAccommodatiom(String Acc_name, String Acc_type,String Acc_address, String Acc_Location, Long Acc_Phoneno, Long Acc_Faxno,
String dis_sea,String dis_airport) throws SQLException {

final String SELECT_KEY_SQL = "select @@id_accomm";
final String sql = "DELETE INTO Accommodation VALUES (?,?,?,?,?,?,?,?)";

try{


Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement pstmt = databaseConnection.prepareStatement(sql);
pstmt.setString(1, Acc_name);
pstmt.setString(2, Acc_type);
pstmt.setString(3, Acc_address);
pstmt.setString(4, Acc_Location);
pstmt.setLong(5, Acc_Phoneno);
pstmt.setLong(6, Acc_Faxno);
pstmt.setString(7, dis_sea);
pstmt.setString(8,dis_airport);
pstmt.executeUpdate();
pstmt.close();

}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void changeAccommodatiom(String Acc_name, String Acc_type,String Acc_address, String Acc_Location, Long Acc_Phoneno, Long Acc_Faxno,
String dis_sea,String dis_airport) throws SQLException {

final String SELECT_KEY_SQL = "select @@id_accomm";
final String sql = "UPDATE INTO Accommodation VALUES (?,?,?,?,?,?,?,?)";

try{


Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement pstmt = databaseConnection.prepareStatement(sql);
pstmt.setString(1, Acc_name);
pstmt.setString(2, Acc_type);
pstmt.setString(3, Acc_address);
pstmt.setString(4, Acc_Location);
pstmt.setLong(5, Acc_Phoneno);
pstmt.setLong(6, Acc_Faxno);
pstmt.setString(7, dis_sea);
pstmt.setString(8,dis_airport);
pstmt.executeUpdate();
pstmt.close();

}
catch (Exception e)
{
e.printStackTrace();
}
}


public static void searchAccommodatiom(String Acc_name, String Acc_type,String Acc_address, String Acc_Location, Long Acc_Phoneno, Long Acc_Faxno,
String dis_sea,String dis_airport) throws SQLException {

final String SELECT_KEY_SQL = "select @@id_accomm";
final String sql = "SELECT INTO Accommodation VALUES (?,?,?,?,?,?,?,?)";

try{


Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement ps = databaseConnection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();

ps.setString(1, Acc_name);
ps.setString(2, Acc_type);
ps.setString(3, Acc_address);
ps.setString(4, Acc_Location);
ps.setLong(5, Acc_Phoneno);
ps.setLong(6, Acc_Faxno);
ps.setString(7, dis_sea);
ps.setString(8,dis_airport);
ps.executeUpdate();
rs.close();
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
}

}

public static void searchRestaurant(String Res_name, String Res_type,String Res_address, String Res_location,
Long Res_Phoneno) throws SQLException {
final String sql= "SELECT INTO Restaurants VALUES(?,?,?,?,?)";
final String SELECT_KEY_SQL = "select @@id_Res";
try{
Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement ps = databaseConnection.prepareStatement(sql);
ps.setString(1, Res_name);
ps.setString(2,Res_type);
ps.setString(3,Res_address);
ps.setString(4,Res_location);
ps.setLong(5,Res_Phoneno);
ps.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void addAttraction(String Att_name, String Att_type,String Att_location,int Ent_fee, String Opening_hours)
throws SQLException{
final String sql= "INSERT INTO Attractions VALUES(?,?,?,?,?)";
final String SELECT_KEY_SQL = "SELECT @@id_Att";
try{
Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement ps = databaseConnection.prepareStatement(sql);
ps.setString(1,Att_name);
ps.setString(2,Att_type);
ps.setString(3,Att_location);
ps.setInt(4,Ent_fee);
ps.setString(5,Opening_hours);
ps.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void changeAttraction( String Att_name, String Att_type,String Att_location,int Ent_fee, String Opening_hours)
throws SQLException{
final String sql= "UPDATE INTO Attractions VALUES(?,?,?,?,?)";
final String SELECT_KEY_SQL = "SELECT @@id_Att";
try{
Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement ps = databaseConnection.prepareStatement(sql);
ps.setString(1,Att_name);
ps.setString(2,Att_type);
ps.setString(3,Att_location);
ps.setInt(4,Ent_fee);
ps.setString(5,Opening_hours);
ps.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
public void searchAttraction( String Att_name, String Att_type,String Att_location,int Ent_fee, String Opening_hours)
throws SQLException{
final String sql= "SELECT INTO Attractions VALUES(?,?,?,?,?)";
final String SELECT_KEY_SQL = "SELECT @@id_Att";
try{
Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement ps = databaseConnection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
ps.setString(1,Att_name);
ps.setString(2,Att_type);
ps.setString(3,Att_location);
ps.setInt(4,Ent_fee);
ps.setString(5,Opening_hours);
rs.close();
ps.close();
} catch (Exception e)
{
e.printStackTrace();
}
}


}




import java.sql.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class modifyTables {

private static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
private static final String BASE_URL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\projec\\proj-1.mdb";


public static void addAccommodatiom(String Acc_name, String Acc_type,String Acc_address, String Acc_Location, Long Acc_Phoneno, Long Acc_Faxno,
String dis_sea,String dis_airport) throws SQLException {

final String SELECT_KEY_SQL = "select @@id_accomm";
final String sql = "INSERT INTO Accommodation VALUES (?,?,?,?,?,?,?,?)";

try{


Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement pstmt = databaseConnection.prepareStatement(sql);
pstmt.setString(1, Acc_name);
pstmt.setString(2, Acc_type);
pstmt.setString(3, Acc_address);
pstmt.setString(4, Acc_Location);
pstmt.setLong(5, Acc_Phoneno);
pstmt.setLong(6, Acc_Faxno);
pstmt.setString(7, dis_sea);
pstmt.setString(8,dis_airport);
pstmt.executeUpdate();
pstmt.close();

}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void addRestaurant (String Res_name, String Res_type,String Res_address, String Res_location,
Long Res_Phoneno) throws SQLException {
final String sql = "INSERT INTO Restaurants VALUES (?,?,?,?,?,?)";
final String SELECT_KEY_SQL = "select @@id_accomm";
try {
Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement pstmt = databaseConnection.prepareStatement(sql);
pstmt.setString(1, Res_name);
pstmt.setString(2,Res_type);
pstmt.setString(3,Res_address);
pstmt.setString(4,Res_location);
pstmt.setLong(5,Res_Phoneno);
pstmt.executeUpdate();
pstmt.close();

} catch (Exception e)
{
e.printStackTrace();
}

}
public static void removeRestaurant(String Res_name, String Res_type,String Res_address, String Res_location,
Long Res_Phoneno) throws SQLException {
final String sql= "DELETE INTO Restaurants VALUES(?,?,?,?,?)";
final String SELECT_KEY_SQL = "select @@id_accomm";
try{
Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement pstmt = databaseConnection.prepareStatement(sql);
pstmt.setString(1, Res_name);
pstmt.setString(2,Res_type);
pstmt.setString(3,Res_address);
pstmt.setString(4,Res_location);
pstmt.setLong(5,Res_Phoneno);
pstmt.close();
} catch (Exception e)
{
e.printStackTrace();
}

}
public static void removeAccommodatiom(String Acc_name, String Acc_type,String Acc_address, String Acc_Location, Long Acc_Phoneno, Long Acc_Faxno,
String dis_sea,String dis_airport) throws SQLException {

final String SELECT_KEY_SQL = "select @@id_accomm";
final String sql = "DELETE INTO Accommodation VALUES (?,?,?,?,?,?,?,?)";

try{


Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement pstmt = databaseConnection.prepareStatement(sql);
pstmt.setString(1, Acc_name);
pstmt.setString(2, Acc_type);
pstmt.setString(3, Acc_address);
pstmt.setString(4, Acc_Location);
pstmt.setLong(5, Acc_Phoneno);
pstmt.setLong(6, Acc_Faxno);
pstmt.setString(7, dis_sea);
pstmt.setString(8,dis_airport);
pstmt.executeUpdate();
pstmt.close();

}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void changeAccommodatiom(String Acc_name, String Acc_type,String Acc_address, String Acc_Location, Long Acc_Phoneno, Long Acc_Faxno,
String dis_sea,String dis_airport) throws SQLException {

final String SELECT_KEY_SQL = "select @@id_accomm";
final String sql = "UPDATE INTO Accommodation VALUES (?,?,?,?,?,?,?,?)";

try{


Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement pstmt = databaseConnection.prepareStatement(sql);
pstmt.setString(1, Acc_name);
pstmt.setString(2, Acc_type);
pstmt.setString(3, Acc_address);
pstmt.setString(4, Acc_Location);
pstmt.setLong(5, Acc_Phoneno);
pstmt.setLong(6, Acc_Faxno);
pstmt.setString(7, dis_sea);
pstmt.setString(8,dis_airport);
pstmt.executeUpdate();
pstmt.close();

}
catch (Exception e)
{
e.printStackTrace();
}
}


public static void searchAccommodatiom(String Acc_name, String Acc_type,String Acc_address, String Acc_Location, Long Acc_Phoneno, Long Acc_Faxno,
String dis_sea,String dis_airport) throws SQLException {

final String SELECT_KEY_SQL = "select @@id_accomm";
final String sql = "SELECT INTO Accommodation VALUES (?,?,?,?,?,?,?,?)";

try{


Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement ps = databaseConnection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();

ps.setString(1, Acc_name);
ps.setString(2, Acc_type);
ps.setString(3, Acc_address);
ps.setString(4, Acc_Location);
ps.setLong(5, Acc_Phoneno);
ps.setLong(6, Acc_Faxno);
ps.setString(7, dis_sea);
ps.setString(8,dis_airport);
ps.executeUpdate();
rs.close();
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
}

}

public static void searchRestaurant(String Res_name, String Res_type,String Res_address, String Res_location,
Long Res_Phoneno) throws SQLException {
final String sql= "SELECT INTO Restaurants VALUES(?,?,?,?,?)";
final String SELECT_KEY_SQL = "select @@id_Res";
try{
Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement ps = databaseConnection.prepareStatement(sql);
ps.setString(1, Res_name);
ps.setString(2,Res_type);
ps.setString(3,Res_address);
ps.setString(4,Res_location);
ps.setLong(5,Res_Phoneno);
ps.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void addAttraction(String Att_name, String Att_type,String Att_location,int Ent_fee, String Opening_hours)
throws SQLException{
final String sql= "INSERT INTO Attractions VALUES(?,?,?,?,?)";
final String SELECT_KEY_SQL = "SELECT @@id_Att";
try{
Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement ps = databaseConnection.prepareStatement(sql);
ps.setString(1,Att_name);
ps.setString(2,Att_type);
ps.setString(3,Att_location);
ps.setInt(4,Ent_fee);
ps.setString(5,Opening_hours);
ps.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void changeAttraction( String Att_name, String Att_type,String Att_location,int Ent_fee, String Opening_hours)
throws SQLException{
final String sql= "UPDATE INTO Attractions VALUES(?,?,?,?,?)";
final String SELECT_KEY_SQL = "SELECT @@id_Att";
try{
Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement ps = databaseConnection.prepareStatement(sql);
ps.setString(1,Att_name);
ps.setString(2,Att_type);
ps.setString(3,Att_location);
ps.setInt(4,Ent_fee);
ps.setString(5,Opening_hours);
ps.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
public void searchAttraction( String Att_name, String Att_type,String Att_location,int Ent_fee, String Opening_hours)
throws SQLException{
final String sql= "SELECT INTO Attractions VALUES(?,?,?,?,?)";
final String SELECT_KEY_SQL = "SELECT @@id_Att";
try{
Connection databaseConnection = connectDatabase.getConnection();
PreparedStatement ps = databaseConnection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
ps.setString(1,Att_name);
ps.setString(2,Att_type);
ps.setString(3,Att_location);
ps.setInt(4,Ent_fee);
ps.setString(5,Opening_hours);
rs.close();
ps.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
import javax.swing.JFrame;
import java.awt.event.ActionEvent;

public class tableGUI extends abstractInputTableDialogue
{

public tableGUI (JFrame mainApp, String[] strList)
{

super(mainApp, strList);
this.setTitle("Create New Table");
cmdClick.setText("Create");

}

public void actionPerformed (ActionEvent e)
{

if (e.getSource() == cmdCancel)
{
this.setVisible(false);
}
else
{

String strTableName = txtTable.getText();

if (!strTableName.equalsIgnoreCase(""))
{

if (!exists(strTableName))
{

strChosenTable = strTableName;
this.setVisible(false);

}
else
{
lblStatus.setText("Table already exists");
}

}

}

}

} public class createTable()
{

public void create(Accommodation acc)
{
PreparedStatement stmt = null;

try
{
stmt = connection.prepareStatement(INSERT_SQL);
stmt.setString(1, book.getTitle());
stmt.setInt(2, book.getYear());
int numRowsAffected = stmt.executeUpdate();
Long id = getKey();
book.setId(id);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
close(stmt);
}
}

public void create(Attraction a)
{
PreparedStatement stmt = null;

try
{
stmt = connection.prepareStatement(INSERT_SQL);
stmt.setString(1, book.getTitle());
stmt.setInt(2, book.getYear());
int numRowsAffected = stmt.executeUpdate();
Long id = getKey();
book.setId(id);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
close(stmt);
}


public void create(Restaurant res)
{
PreparedStatement stmt = null;

try
{
stmt = connection.prepareStatement(INSERT_SQL);
stmt.setString(1, book.getTitle());
stmt.setInt(2, book.getYear());
int numRowsAffected = stmt.executeUpdate();
Long id = getKey();
book.setId(id);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
close(stmt);
}

public Long getKey()
{
Long id = null;
Statement stmt = null;
ResultSet rs = null;

try
{
stmt = connection.createStatement();
rs = stmt.executeQuery(SELECT_KEY_SQL);
while (rs.next())
{
id = Long.valueOf(rs.getLong(1));
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
close(rs);
close(stmt);
}

return id;
}


class Book implements Serializable
{
private Long id;
private String title;
private int year;


public Book(String title, int year)
{
this(null, title, year);
}

public Book(Long id, String title, int year)
{
this.id = id;
this.title = title;
this.year = year;
}

public Long getId()
{
return id;
}

public void setId(Long id)
{
this.id = id;
}


public String getTitle()
{
return title;
}

public int getYear()
{
return year;
}

public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}

Book book = (Book) o;

if (id != null ? !id.equals(book.id) : book.id != null)
{
return false;
}

return true;
}

public int hashCode()
{
return (id != null ? id.hashCode() : 0);
}

public String toString()
{
return new StringBuilder().append("Book{").append("id=").append(id).append(", title='").append(title).append('\'').append(", year=").append(year).append('}').toString();
}
}


}


}


User is offlineProfile CardPM
+Quote Post

cypriot
RE: Creating GUI For Creating Tables
17 Feb, 2007 - 06:46 AM
Post #2

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 2


My Contributions
These codes create a table list and a table name. I need to create a tables with having create statements for their records.
import javax.swing.JFrame;
import java.awt.event.ActionEvent;

public class tableGUI extends abstractInputTableDialogue
{

public tableGUI (JFrame mainApp, String[] strList)
{

super(mainApp, strList);
this.setTitle("Create New Table");
cmdClick.setText("Create");

}

public void actionPerformed (ActionEvent e)
{

if (e.getSource() == cmdCancel)
{
this.setVisible(false);
}
else
{

String strTableName = txtTable.getText();

if (!strTableName.equalsIgnoreCase(""))
{

if (!exists(strTableName))
{

strChosenTable = strTableName;
this.setVisible(false);

}
else
{
lblStatus.setText("Table already exists");
}

}

}

}

}
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:14AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month