Best Projects

Bug Trackingsystem,Books online shopping,college management system,HR management system,Internet banking,Online chat room ,Online exam,Telephone billing system,Banking application,ATM database,airways reservation system,virtual network computing ,calculator,SMTP(simple mail transfer protocol)

GUI For RDBMS

//project main
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.tree.*;
import java.sql.*;
import javax.swing.event.*;

public class ProjectMain extends JFrame implements ActionListener,TreeSelectionListener
{
Container container;
JMenuBar menubar;
JMenu menu;
JMenu New;
JMenu tools;
JMenu tableobj;
JMenuItem Table,database,alter,select,update,delete;

JMenuItem connect;
JMenuItem disconnect;
JMenuItem queryanalyser;

JPanel leftPanel,rightPanel;
JSplitPane splitPane;
JScrollPane scroll,scroll2;
DefaultMutableTreeNode root;
DefaultTreeModel model;
DefaultMutableTreeNode[] nodes;
DefaultMutableTreeNode[] nodes2;
JTree tree;
DatabaseMetaData dbobj ;
DatabaseMetaData dbobj2 ;
ResultSetMetaData rsmd;
ResultSetMetaData rsmd2;
Statement stmnt3;
ResultSet rs;
int colcount;
ResultSetMetaData rstable;
ActionEvent event;
Object objtemp;
ResultSet rs2;
ResultSet rs3;
String ar[];

DatabaseInfo Info = new DatabaseInfo();
Connection cnn2,cnn,con;
Object obj3;
Statement stmnt;
JTextArea text;
JScrollPane txtpanel;
JPanel buttonpanel;
JButton view,insert;
JTable table;
// Appearance ap;
String selected;

int i=0;
public String str,tbname;

ProjectMain()
{
container=getContentPane();

menubar=new JMenuBar();
menu=new JMenu("MAIN");
New=new JMenu("NEW");
database =new JMenuItem("DATABASE");
Table=new JMenuItem("TABLE");
New.add(database);
New.add(Table);
tools=new JMenu("TOOLS");
queryanalyser=new JMenuItem("SHOW QUERY ANALYSER");
tools.add(queryanalyser);
connect=new JMenuItem("CONNECT");
disconnect=new JMenuItem("DISCONNECT");
tableobj=new JMenu("TABLE");
alter=new JMenuItem("ALTER");
select=new JMenuItem("SELECT");
update=new JMenuItem("UPDATE");
delete=new JMenuItem("DELETE");

tableobj.add(alter);
tableobj.add(select);
tableobj.add(update);
tableobj.add(delete);
menu.add(connect);
menu.add(disconnect);
menubar.add(menu);
menubar.add(New);
menubar.add(tools);
menubar.add(tableobj);
setJMenuBar(menubar);

rightPanel=new JPanel();
view=new JButton("VIEW");
insert=new JButton("INSERT");
text=new JTextArea();
txtpanel=new JScrollPane(text);
leftPanel=new JPanel();
leftPanel.setBackground(new Color(120,200,255));
buttonpanel=new JPanel();

buttonpanel.add(view);
buttonpanel.add(insert);
rightPanel.setLayout(new BorderLayout());
rightPanel.setBackground(new Color(120,200,255));
rightPanel.add(txtpanel,BorderLayout.CENTER);
rightPanel.add(buttonpanel,BorderLayout.SOUTH);
view.setBounds(400,100,30,10);
insert.setBounds(440,100,30,10);
splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPanel,rightPanel);
splitPane.setDividerLocation(175);
container.add(splitPane);

connect.addActionListener(this);
disconnect.addActionListener(this);
Table.addActionListener(this);
database.addActionListener(this);
queryanalyser.addActionListener(this);
view.addActionListener(this);
insert.addActionListener(this);
alter.addActionListener(this);
select.addActionListener(this);
update.addActionListener(this);
delete.addActionListener(this);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
try
{
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error :ProjectMain(INIT)"+e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
});
}

public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand().equals("CONNECT"))
{
ConnectionFrame fr=new ConnectionFrame(this);
fr.setSize(270,230);
fr.setLocation(300,200);
fr.setVisible(true);
}
if(event.getActionCommand().equals("VIEW"))
showTableStructure(cnn2);
if(event.getActionCommand().equals("INSERT"))
{
TableInsertion ti=new TableInsertion(this);
Dimension d =Toolkit.getDefaultToolkit().getScreenSize();
ti.setSize(d.width,d.height);
ti.setVisible(true);
}
if(event.getActionCommand().equals("DATABASE"))
{
String tablename="ENTER THE NAME OFTHE DATABASE";
str=JOptionPane.showInputDialog(tablename);
if (str.equals(""))
System.out.println("u clicked cancel");
else
{
DatabaseCreation db=new DatabaseCreation(this);
}
}
if(event.getActionCommand().equals("TABLE"))
{
tbname=JOptionPane.showInputDialog("ENTER THE NAME OF THE TABLE");
if(!tbname.equals(""))
{
TableCreation demo = new TableCreation(this);
demo.pack();
demo.setVisible(true);
}

}

if(event.getActionCommand().equals("SHOW QUERY ANALYSER"))
{
QueryAnalyser qa=new QueryAnalyser ();
Dimension d =Toolkit.getDefaultToolkit().getScreenSize();
qa.setSize(d.width,d.height);
qa.setVisible(true);
}
if(event.getActionCommand().equals("SELECT"))
{
TableSelection tbsel=new TableSelection(this);
tbsel.setLocation(180,50);
tbsel.setSize(630,500);
tbsel.setVisible(true);
}
if(event.getActionCommand().equals("UPDATE"))
{
TableUpdation tbup=new TableUpdation(this);
tbup.setLocation(180,50);
tbup.setSize(400,300);
tbup.setVisible(true);
}
if(event.getActionCommand().equals("ALTER"))
{
TableAlter tbalt=new TableAlter(this);
tbalt.setLocation(180,50);
tbalt.setSize(600,500);
tbalt.setVisible(true);
}
if(event.getActionCommand().equals("DELETE"))
{
TableDelete tbdel=new TableDelete(this);
tbdel.setLocation(180,50);
tbdel.setSize(400,300);
tbdel.setVisible(true);
}
}

public void showTree(Connection con)
{
int i=0;
try
{
leftPanel.setBackground(new Color(120,200,255));
leftPanel.setLayout(new BorderLayout());
root = new DefaultMutableTreeNode("SQL SERVER");
model = new DefaultTreeModel(root);
tree = new JTree(model);
leftPanel.setBackground(new Color(120,200,255));
tree.setForeground(new Color(120,200,255));
tree.setBackground(new Color(120,200,255));
tree.addTreeSelectionListener(this);
dbobj=con.getMetaData();
rs=dbobj.getCatalogs();
while(rs.next())
{
i++;
}
nodes= new DefaultMutableTreeNode[i];
rs=dbobj.getCatalogs();
i=0;
while(rs.next())
{
nodes[i]=new DefaultMutableTreeNode(rs.getString("TABLE_CAT"));
model.insertNodeInto(nodes[i],root,i);
i++;
}
showTables();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"Error :ProjectMain(ST) "+e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}

public void showTables( )
{
for(int i=0;i< nodes.length; i++)
{
try
{
int j=0;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cnn=DriverManager.getConnection("jdbc:odbc:;Server="+Info.info.get("Server")+";Driver=SQL Server;database="+nodes[i]+";user="+Info.info.get("User"));
dbobj2=cnn.getMetaData();
String[] type={"TABLE",};

rs2=dbobj2.getTables(null,null,null,type);
while(rs2.next())
{
j++;
}
nodes2=new DefaultMutableTreeNode[j];

j=0;
rs2=dbobj2.getTables(null,null,null,type);
while(rs2.next())
{
nodes2[j]=new DefaultMutableTreeNode(rs2.getString("TABLE_NAME"));
model.insertNodeInto(nodes2[j],nodes[i],j);
j++;
}
scroll = new JScrollPane(tree);
leftPanel.add(scroll,BorderLayout.CENTER);
leftPanel.updateUI();
model.reload();
}
catch(Exception e1)
{
JOptionPane.showMessageDialog(this,"Error :ProjectMain(STables) "+e1.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
}

public void showTableStructure(Connection stcon)
{
System.out.println("first");

try
{
System.out.println("inside try");
stmnt=stcon.createStatement();
rs3=stmnt.executeQuery("select * from "+selected);
rsmd=rs3.getMetaData();
colcount=rsmd.getColumnCount();
}
catch(Exception e1)
{
JOptionPane.showMessageDialog(this,"Error :ProjectMain(STS) "+e1.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
rightPanel.remove(txtpanel);
int l=0;
int k=0;
try
{
while(rs3.next())
{
l++;
}
String head[]=new String[colcount];
String data[][]=new String[l][colcount];
for(k=0;k {
head[k]=rsmd.getColumnLabel(k+1);
}
stmnt.close();
int row=0;
stmnt=stcon.createStatement();
rs3=stmnt.executeQuery("select * from "+selected);
while(rs3.next())
{
for(k=0;k {
data[row][k]=rs3.getString(head[k]);
}
row++;
}
stmnt.close();
table=new JTable(data,head);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
txtpanel=new JScrollPane(table,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
rightPanel.add(txtpanel,BorderLayout.CENTER);
rightPanel.validate();
}
catch(Exception ex4)
{
JOptionPane.showMessageDialog(this,"Error :ProjectMain(STS) "+ex4.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}

public void valueChanged(TreeSelectionEvent event2)
{
try
{
rightPanel.remove(txtpanel);
text=new JTextArea(20,40);
txtpanel=new JScrollPane(text);
rightPanel.add(txtpanel,BorderLayout.CENTER);
rightPanel.validate();
TreePath tp =event2.getNewLeadSelectionPath();
objtemp=tp.getLastPathComponent();
selected=tp.getLastPathComponent().toString();
TreePath tp1 =tp.getParentPath();
obj3=tp1.getLastPathComponent();

if (obj3==root)
{
for(i=0;i {
if(objtemp==nodes[i])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cnn=DriverManager.getConnection("jdbc:odbc:;Server="+Info.info.get("Server")+";Driver=SQL Server;database="+nodes[i]+";user="+Info.info.get("User"));
Statement datst=cnn.createStatement();
ResultSet rsdat=datst.executeQuery(" sp_helpdb "+objtemp.toString());
//datst.close();
while(rsdat.next())
{
text.append(" NAME: "+rsdat.getString(1)+"\n");
text.append(" DBSIZE: "+rsdat.getString(2)+"\n");
text.append("OWNER : "+rsdat.getString("owner")+"\n");
text.append("DBID: "+rsdat.getString("dbid")+"\n");
text.append("CREATED: "+rsdat.getString("created")+"\n");
text.append("STATUS: "+rsdat.getString("status")+"\n");
}
datst.close();
}
catch(Exception f)
{
JOptionPane.showMessageDialog(this,"Error :ProjectMain(TVC) "+f.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
}
else
{
System.out.println("selected table");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cnn2=DriverManager.getConnection("jdbc:odbc:;Server="+Info.info.get("Server")+";Driver=SQL Server;database="+obj3.toString()+";user="+Info.info.get("User"));
}

}
catch(Exception ex2)
{
JOptionPane.showMessageDialog(this,"Error :ProjectMain(TVC) "+ex2.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}

public static void main(String a[])
{
ProjectMain pm=new ProjectMain();
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
pm.setSize(d.width,d.height);
pm.setVisible(true);
}
}



//Data Base Creation
import javax.swing.tree.*;
import java.sql.*;
public class DatabaseCreation
{

ProjectMain pm;
DatabaseCreation(ProjectMain pm)
{
try
{
this.pm = pm;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cnn=DriverManager.getConnection("jdbc:odbc:;Server="+pm.Info.info.get("Server")+";Driver=SQL Server;database=;user="+pm.Info.info.get("User"));
System.out.println("INSIDE TRY server"+pm.Info.info.get("Server"));
Statement st=cnn.createStatement();
System.out.println("value os string"+pm.str);

st.executeUpdate("create database "+pm.str);
System.out.println("database created");
System.out.println("DRIVER O.K");
st.close();
cnn.close();
//pm.i++;
//pm.nodes[i]=new DefaultMutableTreeNode[pm.str];
pm.leftPanel.removeAll();
pm.showTree(pm.con);
//pm.model.insertNodeInto(new DefaultMutableTreeNode(pm.str),pm.root,pm.tree.getRowCount()-1);
pm.model.reload();
}
catch(Exception e)
{
System.out.println("database creation fails");
}

}
}






//Add Servers
import javax.swing.*;
import java.awt.*;
import java.sql.*;

public class AddServers extends JFrame
{
String newserver;
Connection servcon;
Statement servst;
DatabaseInfo Info3 = new DatabaseInfo();

public AddServers(JComboBox c)
{
//this.fr=fr;
newserver=JOptionPane.showInputDialog("Enter the name of the new server");
if (newserver==null)
System.out.println("Please check the new name of the server");
else
{ try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
servcon=DriverManager.getConnection("jdbc:odbc:;Server=L1O0M5;Driver=SQL Server;database=gui;user=sa");

servst=servcon.createStatement();
servst.executeUpdate("insert into serverinfo values('"+newserver+"')");
}catch(Exception e1)
{
e1.printStackTrace();
}
c.addItem(newserver);
System.out.println("new server added ");
}
}

}




//Appearance
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;

class Appearance extends JFrame

{
Container container;
JPanel panel;
JButton button;
public BevelBorder b1,b2;
public Font f1,f2,f3,f4;
JTextField t,t2,t3;
public Color col1,col2,col3,col4;
Appearance()
{
container=getContentPane();

panel=new JPanel();
t=new JTextField(20);
t2=new JTextField(10);
t3=new JTextField(5);
button=new JButton("HELLO");
b1=new BevelBorder(BevelBorder.RAISED,Color.white,Color.white);
// b2=new BevelBorder(BevelBorder.RAISED,Color.blue,Color.blue);
//button.setBorder(b1);
button.setFont(f1);
f1=new Font("timesnewroman",Font.BOLD,22);
f2=new Font("timesnewroman",Font.BOLD,16);
f3=new Font("tahoma",Font.PLAIN,12);
f4=new Font("tahoma",Font.BOLD,14);
t.setFont(f2);
t2.setFont(f4);
col1=new Color(100,150,254);
col2=new Color (20,150,254);
col3=new Color (60,120,254);
col4=new Color (28,75,255);
t.setBackground(Color.white);
b2=new BevelBorder(BevelBorder.RAISED,col4,col4);
button.setBorder(b1);
t.setBorder(b2);
//t.setForeground(Color.white);
t2.setBackground(col2);
t2.setForeground(Color.white);
t2.setBorder(b1);
t3.setBackground(col4);

button.setForeground(col2);
//t3.setBackground(Color.white);
button.setBackground(Color.blue);
//t2.setForeground(col2);
button.setForeground(Color.white);
panel.add(button);

panel.add(t);
panel.add(t2);
panel.add(t3);
//container.setBackground(col1);
panel.setBackground(col2);

container.add(panel);
}
public static void main(String a[])
{
Appearance ap=new Appearance();
ap.setLocation(300,250);
ap.setSize(400,400);
ap.setVisible(true);
}


}



//Connection Frame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class ConnectionFrame extends JDialog implements ActionListener
{
JLabel serverlb;
JLabel userlb;
JLabel passwordlb;
JLabel rdbmslb;
JComboBox servername;
JTextField username;
JPasswordField password;
JComboBox rdbms;
// JTextField database;
JPanel panel;
JButton connect;
JButton a;
Container container;
Connection cnn;
Connection servcon;
Statement servst;
ResultSet servrs;
ProjectMain pm;
ConnectionFrame(ProjectMain pm)
{
super(pm,true);
setTitle("CONNECTION SECTION");
this.pm=pm;
container=getContentPane();
JPanel panel=new JPanel();
panel.setLayout(null);
panel.setBackground(new Color(120,200,255));
serverlb=new JLabel("SERVER NAME");
userlb=new JLabel("USER");
passwordlb=new JLabel("PASSWORD");
rdbmslb=new JLabel("RDBMS");
//databaselb=new JLabel("DATABASE NAME");
servername=new JComboBox();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
servcon=DriverManager.getConnection("jdbc:odbc:;Server=STATION8;Driver=SQL Server;database=gui;user=sa");
servst=servcon.createStatement();
servrs=servst.executeQuery("select * from serverinfo");
while(servrs.next())
{
//System.out.println("servername"+servrs.getString(1));
/* String server=servrs.getString(1);
;

servername.addItem(server);*/

servername.addItem(servrs.getString(1));
}

}catch(Exception e1){System.out.println("cannot connect to serverinfo"+e1);}




username=new JTextField();
password=new JPasswordField();
rdbms=new JComboBox();
rdbms.addItem("MSSQL");
rdbms.addItem("ORACLE");
rdbms.addItem("MSACCESS");
// database=new JTextField();
connect=new JButton("Connect");
a=new JButton("addserv");

rdbmslb.setBounds(40,20,90,50);
serverlb.setBounds(40,45,90,50);
userlb.setBounds(40,80,90,40);
passwordlb.setBounds(40,105,90,40);
//databaselb.setBounds(80,160,60,40);
servername.setBounds(145,63,100,20);
username.setBounds(145,90,100,20);
password.setBounds(145,115,100,20);
rdbms.setBounds(145,35,100,20);
//database.setBounds(145,170,80,20);
connect.setBounds(65,155,90,20);
a.setBounds(150,155,80,20);
panel.add(rdbmslb);
panel.add(rdbms);
panel.add(serverlb);
panel.add(servername);
panel.add(userlb);
panel.add(passwordlb);


panel.add(username);
panel.add(password);
// panel.add(database);
panel.add(connect);
panel.add(a);
container.add(panel);
a.addActionListener(this);
connect.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{

try
{
if(event.getActionCommand().equals("Connect"))
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cnn=DriverManager.getConnection("jdbc:odbc:;Server="+ servername.getSelectedItem().toString() + ";Driver=SQL Server;database=;user="+ username.getText()+";password="+password.getText());
pm.con = cnn;
pm.Info.info.put("Server",servername.getSelectedItem().toString());
pm.Info.info.put("User",username.getText());
pm.Info.info.put("Password",password.getText());
System.out.println("connection established"+pm.Info.info.get("Server")+":"+pm.Info.info.get("User"));
dispose();
pm.showTree(cnn);
pm.leftPanel.updateUI();
}
if(event.getActionCommand().equals("addserv"))
{
AddServers g = new AddServers(servername);
}
} catch(Exception ex){System.out.println("Connection Failed");}
}
};




//DataBaseCreation
import javax.swing.tree.*;
import java.sql.*;

public class DatabaseCreation
{

ProjectMain pm;
DatabaseCreation(ProjectMain pm)
{
try
{
this.pm = pm;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cnn=DriverManager.getConnection("jdbc:odbc:;Server="+pm.Info.info.get("Server")+";Driver=SQL Server;database=;user="+pm.Info.info.get("User"));
System.out.println("INSIDE TRY server"+pm.Info.info.get("Server"));
Statement st=cnn.createStatement();
System.out.println("value os string"+pm.str);

st.executeUpdate("create database "+pm.str);
System.out.println("database created");
System.out.println("DRIVER O.K");
st.close();
cnn.close();
//pm.i++;
//pm.nodes[i]=new DefaultMutableTreeNode[pm.str];
pm.leftPanel.removeAll();
pm.showTree(pm.con);
//pm.model.insertNodeInto(new DefaultMutableTreeNode(pm.str),pm.root,pm.tree.getRowCount()-1);
pm.model.reload();
}
catch(Exception e)
{
System.out.println("database creation fails");
}

}
}



//DataBaseInfo
import java.util.*;

public class DatabaseInfo
{
Hashtable info;

public DatabaseInfo()
{
info = new Hashtable();
}
}




//Login
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
class login1 extends JFrame implements ActionListener
{
JLabel lbwelcome;
JLabel lblogin;
JLabel lbpassword;
JTextField txtlogin;
JTextField txtpassword;
JButton ok;
JButton cancel;
Container container;
JPanel panel;
Color col;
public Color col1;
BevelBorder br;
LineBorder lb;
login1()
{
super("LOGIN");
container=getContentPane();
JPanel panel=new JPanel();
panel.setLayout(null);

col=new Color(120,200,255);
lb=new LineBorder(Color.black,2);
br=new BevelBorder(BevelBorder.RAISED);
panel.setBackground(col);
panel.setBorder(lb);
JLabel lbwelcome =new JLabel("YOU ARE IN LOGIN SECTI0N");

JLabel lblogin=new JLabel("LOGIN");
JLabel lbpassword=new JLabel("PASSWORD");
JButton b1=new JButton("O.K");
JButton b2=new JButton("CANCEL");
txtlogin=new JTextField();
txtpassword=new JTextField(10);
lbwelcome.setBounds(110,20,160,50);
lbwelcome.setForeground(Color.black);
lblogin.setForeground(Color.black);
lbpassword.setForeground(Color.black);
lblogin.setBounds(65,70,70,20);
lbpassword.setBounds(65,110,70,20);
txtlogin.setBounds(165,70,120,20);
txtpassword.setBounds(165,110,120,20);
b1.setBounds(100,162,75,20);
b2.setBounds(185,162,75,20);
b1.setBorder(br);
b2.setBorder(br);
b2.addActionListener(this);
panel.add(lbwelcome);
panel.add(lblogin);
panel.add(lbpassword);
panel.add(txtlogin);
panel.add(txtpassword);
panel.add(b1);
panel.add(b2);
container.add(panel);
}
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
public Color newcolor()
{
col1=new Color(120,200,255);
return(col1);
}

}
class login
{
public static void main(String a[])
{
login1 obj=new login1();
obj.setSize(345,240);
obj.setLocation(200,125);
obj.setVisible(true);
obj.setResizable(false);
}
}



//Table Alter
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.sql.*;
class TableAlter extends JDialog implements ItemListener,ActionListener
{
JCheckBox[] fields;
JRadioButton addcol,addconst,dropcol,dropconst,constraint,column,datatype;
Container container;
JPanel leftpanel,rightpanel1,rightpanel2,rightpanel3;
JLabel lab1,lab2,lab3;
JTextField text1,text2,text3,text4;
ButtonGroup bg1,bg2;
Connection tbaltcon;
Statement tbaltst;
ResultSet tbaltrs;
ResultSetMetaData tbaltrsmd;
JSplitPane jsp1,jsp2,jsp3,jsp4,jsp5;
JButton ok,cancel;

ProjectMain pm;
int colcount;
TableAlter(ProjectMain pm)
{
super(pm,true);
setTitle("ALTER TABLE");
this.pm=pm;
container=getContentPane();
leftpanel=new JPanel();
leftpanel.setLayout(null);
rightpanel1=new JPanel();
rightpanel1.setLayout(null);
rightpanel2=new JPanel();
rightpanel2.setLayout(null);
rightpanel3=new JPanel();
rightpanel3.setLayout(null);


bg1=new ButtonGroup();
bg2=new ButtonGroup();


text1=new JTextField();
text1.setBounds(160,20,120,30);
text1.setEnabled(false);
text2=new JTextField();
text2.setBounds(160,60,120,30);
text2.setEnabled(false);
addcol=new JRadioButton ("ADD COLUMN");
bg1.add(addcol);
addconst=new JRadioButton ("ADD CONSTRAINT");
bg1.add(addconst);
addcol.setBounds(40,20,120,20);
addconst.setBounds(40,70,120,20);
addcol.addItemListener(this);
addconst.addItemListener(this);
rightpanel1.add(addcol);
rightpanel1.add(addconst);
rightpanel1.add(text1);
rightpanel1.add(text2);


dropcol=new JRadioButton("DROP COLUMN");
dropcol.setBounds(40,20,120,20);
bg2.add(dropcol);
dropconst=new JRadioButton("DROP CONSTRAINT");
dropconst.setBounds(40,70,135,20);
bg2.add(dropconst);
dropcol.addItemListener(this);
dropconst.addItemListener(this);
text3=new JTextField();
text3.setBounds(170,70,120,30);
text3.setEnabled(false);
rightpanel2.add(dropcol);
rightpanel2.add(dropconst);
rightpanel2.add(text3);
datatype=new JRadioButton("ALTER DATATYPE");
datatype.setBounds(40,20,130,20);
datatype.addItemListener(this);
rightpanel3.add(datatype);
text4=new JTextField();
text4.setBounds(170,20,120,30);
text4.setEnabled(false);
rightpanel3.add(text4);
lab1=new JLabel();
lab2=new JLabel();
lab3=new JLabel("enter the new colname/const in the corresponding text");
lab3.setBounds(20,15,200,20);
lab3.setVisible(false);

lab2.setBounds(170,20,160,20);
rightpanel2.add(lab2);
lab2.setVisible(false);
lab1.setBounds(170,50,160,20);
rightpanel2.add(lab1);
lab1.setVisible(false);

ok=new JButton("O.K");
ok.addActionListener(this);
ok.setBounds(35,300,70,20);

leftpanel.add(ok);
cancel=new JButton("CANCEL");
cancel.addActionListener(this);
cancel.setBounds(110,300,80,20);
leftpanel.add(cancel);
try
{
tbaltcon=pm.cnn2;
tbaltst=tbaltcon.createStatement();
tbaltrs=tbaltst.executeQuery("select * from "+pm.selected);
tbaltrsmd=tbaltrs.getMetaData();
colcount=tbaltrsmd.getColumnCount();
int loc=40;
fields=new JCheckBox[colcount];
for(int i=1;i<=colcount;i++)
{

fields[i-1]=new JCheckBox(tbaltrsmd.getColumnLabel(i));
fields[i-1].setBounds(40,loc,120,20);

loc=loc+40;
leftpanel.add(fields[i-1]);
}
leftpanel.setEnabled(false);
}catch(Exception altex){System.out.println("cannot connect to the table");}
jsp1=new JSplitPane(JSplitPane.VERTICAL_SPLIT,leftpanel,rightpanel1);
jsp1.setDividerLocation(175);
jsp2=new JSplitPane(JSplitPane.VERTICAL_SPLIT,rightpanel1,rightpanel2);
jsp2.setDividerLocation(120);
jsp3=new JSplitPane(JSplitPane.VERTICAL_SPLIT,rightpanel2,rightpanel3);
jsp3.setDividerLocation(150);
jsp4=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jsp2,jsp3);
jsp4.setDividerLocation(150);
jsp5=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jsp1,jsp4);
jsp5.setDividerLocation(200);
container.add(jsp5);


}
public void itemStateChanged(ItemEvent event1)
{





if(addcol.isSelected())
{
text1.setEnabled(true);
dropcol.setEnabled(false);
dropconst.setEnabled(false);
datatype.setEnabled(false);
lab3.setVisible(true);

for(int i=0;i {
fields[i].setEnabled(false);

}

}
if(addconst.isSelected())
{
text2.setEnabled(true);
dropcol.setEnabled(false);
dropconst.setEnabled(false);
datatype.setEnabled(false);


for(int i=0;i {
fields[i].setEnabled(false);

}

}

if(dropcol.isSelected())
{
addcol.setEnabled(false);
addconst.setEnabled(false);
text1.setEnabled(false);
text2.setEnabled(false);
text3.setEnabled(false);
text4.setEnabled(false);
for(int i=0;i {
fields[i].setEnabled(true);
}
lab1.setVisible(false);
lab2.setText("Select a column to drop");
lab2.setVisible(true);
}

if(dropconst.isSelected())
{
text1.setEnabled(false);
text2.setEnabled(false);
text4.setEnabled(false);
text3.setEnabled(true);
for(int i=0;i {
fields[i].setEnabled(false);
}
lab2.setVisible(false);
lab1.setText("Enter The Constraint Name ");
lab1.setVisible(true);
}

if(datatype.isSelected())
{
addcol.setEnabled(false);
addconst.setEnabled(false);
dropcol.setEnabled(false);
dropconst.setEnabled(false);
text1.setEnabled(false);
text2.setEnabled(false);
text3.setEnabled(false);
text4.setEnabled(true);
for(int i=0;i {
fields[i].setEnabled(true);
}
}

}






public void actionPerformed(ActionEvent event2)
{
if(event2.getSource()==cancel)
dispose();

else
{

String str=new String();
String str1=new String();
int count=0;
str="alter table "+pm.selected;
if(addcol.isSelected()&&!text1.getText().equals(""))
str=str+" add "+text1.getText();
if(addconst.isSelected()&&!text2.getText().equals(""))
str=str+" add constraint "+text2.getText();
if(dropcol.isSelected())
{
str1="";
for(int i=0;i {
if( fields[i].isSelected())
{
count++;
if (count>1)
str1=str1+",";
str1=str1+fields[i].getText();
}
}

str=str+" drop column "+str1;
}
if(dropconst.isSelected())
str=str+" drop constraint "+text3.getText();
if(datatype.isSelected())
str=str+" alter column "+text4.getText();
System.out.println("string"+str);
try{
tbaltst.executeUpdate(str);
System.out.println("altered");
}
catch(Exception e)
{
String m=e.getMessage();
int len=m.length();
m=m.substring(0,len/4-1)+"\n"+m.substring(len/4-1,len/2-1)+"\n"+m.substring(len/2-1,len*3/4-1)+"\n"+m.substring(len*3/4-1,len-1);
JOptionPane.showMessageDialog(this,m);
}
}
}



}




//querry analyser
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.table.*;

class QueryAnalyser extends JFrame implements ActionListener
{

JTextArea executionpane;
JSplitPane splitpane;
JScrollPane scroll1;
JPanel panel1,panel2;
JTabbedPane resultPane;
JButton execute;
JButton clearresultpane;
JComboBox databasecombo;
Connection qacnn,qacnn2;
DatabaseMetaData qadbobj ;
ResultSetMetaData qarsmd;
ResultSet qars;
Vector head,data;
RecordSet records = null;

QueryAnalyser()
{
super("WELCOME TO QUERY ANALYSER");
Container container=getContentPane();

Font f=new Font("Tahoma",Font.BOLD,12);
panel1=new JPanel();
panel2=new JPanel();
resultPane = new JTabbedPane();
panel1.setLayout(new BorderLayout());

JButton execute=new JButton("EXECUTE");
execute.setBounds(200,30,80,20);
clearresultpane=new JButton("CLEAR QUERYPANE");
clearresultpane.setBounds(100,30,80,20);
executionpane=new JTextArea();
executionpane.setFont(f);
databasecombo=new JComboBox();

panel2.add( databasecombo);
databasecombo.setBounds(145,30,80,20);
panel2.add(clearresultpane);
panel2.add(execute);
panel1.add(panel2,BorderLayout.NORTH);

int vsb = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ;
int hsb =JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS ;
scroll1=new JScrollPane(executionpane,vsb,hsb);
splitpane=new JSplitPane(JSplitPane.VERTICAL_SPLIT,scroll1,resultPane);
panel1.add(splitpane,BorderLayout.CENTER);
splitpane.setDividerLocation(175);
container.add(panel1);
execute.addActionListener(this);
clearresultpane.addActionListener(this);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
qacnn=DriverManager.getConnection("jdbc:odbc:;Server=STATION8;Driver=SQL Server;database=;user=sa;password=;");
qadbobj=qacnn.getMetaData();
qars=qadbobj.getCatalogs();
while(qars.next())
{
databasecombo.addItem(qars.getString("TABLE_CAT"));
}
}
catch(Exception qaex)
{
System.out.println("exception"+qaex);
}
}

public void actionPerformed(ActionEvent event)
{
int colcnt=0;
if (event.getActionCommand().equals("EXECUTE"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
qacnn2=DriverManager.getConnection("jdbc:odbc:;Server=STATION8;Driver=SQL Server;database="+databasecombo.getSelectedItem().toString()+";user=sa;password=;");
Statement st=qacnn2.createStatement();
if(event.getActionCommand().equals("EXECUTE"))
{
String qas=executionpane.getSelectedText();
if(st.execute(qas))
{
while(true)
{
qars = st.getResultSet();
if(qars != null)
{
qarsmd=qars.getMetaData();
colcnt=qarsmd.getColumnCount();
head = new Vector();
int k = 0;
for(k=1;k<=colcnt;k++)
{
head.add(k-1,qarsmd.getColumnLabel(k));
}
data = new Vector()
{



public Object elementAt(int index)
{
return get(index);
}
};
k = 0;
while(qars.next())
{
RecordSet records = new RecordSet(head);
for(int col =1;col<=colcnt;col++)
{
records.setData(col-1,qars.getString(col));
}
data.set(k,records);
k++;
}

JTable table = new JTable(data,head);
JScrollPane panel = new JScrollPane(table);
resultPane.addTab("Query Result ",panel);
}
if(!st.getMoreResults() || (st.getUpdateCount()!=-1))
break;
}

}
else
{
}
}
}
catch(Exception ex)
{
System.out.println("Exception"+ex);
}
}
if (event.getActionCommand().equals("CLEAR QUERYPANE"))
{
executionpane.setText("");
}
}//action
}

class RecordSet extends Vector
{
Vector head;

public RecordSet(Vector head)
{
this.head = head;
}

public void setData(int index,String object)
{
set(index,object);
}

public Object elementAt(int index)
{
return this.get(index);
}
}





//Table Creation
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;

public class TableCreation extends JDialog implements ActionListener
{
Container contentPane;
JScrollPane scroll;
JTable table;
JComboBox typecombo;
Vector column = new Vector();
Vector rows = new Vector();
RowVector rowVector;
int tot_records = 100;
Connection tbcon1;
Statement tbst1,tbst2;
ResultSet tbrs1;
String array[][];
JButton create;
int count,primcount=0;
String primstr;
ProjectMain pm;

public TableCreation(ProjectMain pm)
{
super(pm,true);
try
{
this.pm=pm;
System.out.println("hello");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
tbcon1=DriverManager.getConnection("jdbc:odbc:;Server=station8;Driver=SQL Server;database=master;user=sa");
typecombo = new JComboBox();

typecombo.addItem("-- Data Types --");
tbst1=tbcon1.createStatement();
tbrs1=tbst1.executeQuery("select * from systypes");
while(tbrs1.next())
{
typecombo.addItem(tbrs1.getString(1));
}
tbst1.close();
}
catch(Exception tbex1)
{
JOptionPane.showMessageDialog(this,"Error :TableCreation(INIT) "+tbex1.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
create=new JButton("CREATE");
create.addActionListener(this);
column.add(0,"Field Name");
column.add(1,"Data Type");
column.add(2,"Size");
column.add(3,"Primary Key");
column.add(4,"Unique");
column.add(5,"Not Null");
column.add(6,"Check");
column.add(7,"Default");
primstr=new String();
primstr="";

for(int i = 0;i < 100;i++)
{
rowVector = new RowVector();
rows.add(i,rowVector);
}

TableModel model = new AbstractTableModel()
{
public int getRowCount()
{
return tot_records;
}

public int getColumnCount()
{
return column.size();
}

public String getColumnName(int col)
{
return column.get(col).toString();
}

public Object getValueAt(int row,int col)
{

if(col == 2)
{
String typestr=getValueAt(row,1).toString();
if(typestr.equals("binary")||typestr.equals("char")||typestr.equals("nchar"))
{
setValueAt(new Integer(10),row,2);
}
if(typestr.equals("int")||typestr.equals("smallmoney")||typestr.equals("smalldatetime")||typestr.equals("real"))
{
setValueAt(new Integer(4),row,2);
}
if(typestr.equals("varchar")||typestr.equals("nvarchar")||typestr.equals("varbinary"))
{
setValueAt(new Integer(50),row,2);
}
if(typestr.equals("float")||typestr.equals("money")||typestr.equals("timestamp")||typestr.equals("datetime"))
{
setValueAt(new Integer(8),row,2);
}
if(typestr.equals("text")||typestr.equals("ntext")||typestr.equals("image")||typestr.equals("uniqueidentifier"))
{
setValueAt(new Integer(16),row,2);
}
if(typestr.equals("decimal")||typestr.equals("numeric"))
{
setValueAt(new Integer(9),row,2);
}
if(typestr.equals("bit")||typestr.equals("tinyint"))
{
setValueAt(new Integer(1),row,2);
}
if(typestr.equals("smallint"))
{
setValueAt(new Integer(1),row,2);
}

fireTableDataChanged();
}

return ((RowVector)rows.elementAt(row)).elementAt(col);

}

public void setValueAt(Object ob,int row,int col)
{
((RowVector)rows.get(row)).setElementAt(ob,col);

}

public Class getColumnClass(int col)
{
return getValueAt(0, col).getClass();

}

public boolean isCellEditable(int row, int col)
{

if(col==2)
{
String str=getValueAt(row,1).toString();

if(str.equals("binary")||str.equals("nbinary")||str.equals("varchar")||str.equals("nvarchar")||str.equals("char")||str.equals("nchar"))
{
return true;
}
else
return false;
}
if(col==4)
{
if(((Boolean)getValueAt(row,3)).booleanValue()||((Boolean)getValueAt(row,5)).booleanValue())
return false;

}
if(col==3)
{
if(((Boolean)getValueAt(row,4)).booleanValue()||((Boolean)getValueAt(row,5)).booleanValue())
return false;
}
if(col==5)
{
if(((Boolean)getValueAt(row,3)).booleanValue()||((Boolean)getValueAt(row,4)).booleanValue())
return false;
}
return true;
}

};

table = new JTable(model);
JTextField sizetext=new JTextField("");
TableColumn classColumn1 = table.getColumn("Data Type");
classColumn1.setCellEditor(new DefaultCellEditor(typecombo));
TableColumn classColumn2=table.getColumn("Size");
classColumn2.setCellEditor(new DefaultCellEditor(sizetext));
scroll = new JScrollPane(table);
scroll.setPreferredSize(new Dimension(400,400));

contentPane.add(scroll,BorderLayout.CENTER);
contentPane.add(create,BorderLayout.SOUTH);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
dispose();
}
});
}

public void actionPerformed(ActionEvent e)
{
String str1=new String();
String str3=new String();
str3="create table "+pm.tbname+" (";
int strcount=0;
count=0;
int prloc=0,rowcount = 0;

for(int row=0;row<100;row++)
{
if(!((RowVector)rows.elementAt(row)).elementAt(0).toString().equals(""))
rowcount++;
}
array = new String[rowcount][8];

for(int row=0;row<100;row++)
{
if(!((RowVector)rows.elementAt(row)).elementAt(0).toString().equals(""))
{
array[count][0]=((RowVector)rows.elementAt(row)).elementAt(0).toString();
array[count][1]=((RowVector)rows.elementAt(row)).elementAt(1).toString();
String str2=((RowVector)rows.elementAt(row)).elementAt(1).toString();
if(str2.equals("binary")||str2.equals("char")||str2.equals("nchar")||str2.equals("varchar")||str2.equals("nvarchar")||str2.equals("varbinary"))
array[count][2]="("+((RowVector)rows.elementAt(row)).elementAt(2).toString()+")";
else
array[count][2]=" ";
if(((Boolean)((RowVector)rows.elementAt(row)).elementAt(3)).booleanValue())
{
primcount++;
if(primcount>1)
primstr=primstr +",";
else
primstr=",primary key (";
array[count][3]="";
primstr=primstr+array[count][0];
}//if boolean
else
array[count][3]="";
if(((Boolean)((RowVector)rows.elementAt(row)).elementAt(4)).booleanValue()==true)
array[count][4]="unique";//unique constr
else
array[count][4]="";
if(((Boolean)((RowVector)rows.elementAt(row)).elementAt(5)).booleanValue()==true)
array[count][5]="not null";//not null constr
else
array[count][5]="";
if(!((RowVector)rows.elementAt(row)).elementAt(6).toString().trim().equals(""))
array[count][6]="check("+ array[count][0]+((RowVector)rows.elementAt(row)).elementAt(6).toString()+")";
else
array[count][6]="";
if(!((RowVector)rows.elementAt(row)).elementAt(7).toString().trim().equals(""))
array[count][7]="default"+((RowVector)rows.elementAt(row)).elementAt(7).toString();
else
array[count][7]="";
strcount++;
if(strcount>1)
str1=str1+",";
str1=str1+array[count][0]+" "+array[count][1]+" "+array[count][2]+" "+array[count][3]+" "+array[count][4]+" "+array[count][5]+" "+array[count][6];

count++;
}//if row vector
}//for

if(!primstr.equals(""))
primstr=primstr+")";
for(int i=0;i {
System.out.println("array"+array[i][0]+" "+ array[i][1]+" "+array[i][2]+" "+array[i][3]+" "+array[i][4]+" "+array[i][5]+" "+array[i][6]+" "+array[i][7]);
}
System.out.println("primary string"+primstr);
System.out.println("string"+str1);
String s=new String();
if(primstr.equals(""))
s=str3+str1;
else
s=str3+str1+primstr;
System.out.println("create string "+s+")");
try
{
Connection createcon=pm.cnn;
tbst2=createcon.createStatement();
tbst2.executeUpdate(s+")");
tbst2.close();
dispose();
pm.leftPanel.removeAll();
pm.showTree(pm.con);
JOptionPane.showMessageDialog(null,"Table Created","Success",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex3)
{
String m=ex3.getMessage();
int len=m.length();
m=m.substring(0,len/4-1)+"\n"+m.substring(len/4-1,len/2-1)+"\n"+m.substring(len/2-1,len*3/4-1)+"\n"+m.substring(len*3/4-1,len-1);
JOptionPane.showMessageDialog(null,"Error :TableCreation"+ex3.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}

}//action performed
}

class RowVector extends Vector
{
RowVector()
{
add(0,new String(""));
add(1,new String(" "));
add(2,new Integer("0"));
add(3,new Boolean(" "));
add(4,new Boolean(false));
add(5,new Boolean(false));
add(6,new String(" "));
add(7,new String(" "));
}
}




//Table Delete
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
class TableDelete extends JDialog implements ItemListener,ActionListener
{
Container container;
JPanel upperpanel,lowerpanel;
JCheckBox[] fields;
Connection tbdelcon;
Statement tbdelst;
ResultSet tbdelrs;
ResultSetMetaData tbdelrsmd;
JTextField condition;
JRadioButton conditionbutton;
JButton ok,cancel;
JLabel label;
int colcount;
ProjectMain pm;
TableDelete(ProjectMain pm)
{
super(pm,true);
setTitle("TABLE DELETION");
this.pm=pm;
container=getContentPane();
upperpanel=new JPanel();
lowerpanel=new JPanel();
JSplitPane jsp=new JSplitPane(JSplitPane.VERTICAL_SPLIT,upperpanel,lowerpanel);
jsp.setDividerLocation(120);
upperpanel.setLayout(null);
lowerpanel.setLayout(null);
ok=new JButton("O.K");
ok.addActionListener(this);
cancel=new JButton("CANCEL");
cancel.addActionListener(this);
//ok.setBounds(100,50,80,30);
//cancel.setBounds(190,50,80,30);
conditionbutton=new JRadioButton("SPECIFY CONDITION FOR DELETION IF ANY");
conditionbutton.setBounds(70,20,270,20);
conditionbutton.addItemListener(this);
condition=new JTextField();
condition.setBounds(90,40,235,20);
condition.setEnabled(false);
ok.setBounds(90,80,80,30);
cancel.setBounds(170,80,80,30);
label=new JLabel("SELECT THE FIELDS TO BE DELETED");
label.setBounds(50,15,270, 20);
upperpanel.add(label);
lowerpanel.add(conditionbutton);
lowerpanel.add(condition);

lowerpanel.add(ok);
lowerpanel.add(cancel);
try
{
tbdelcon=pm.cnn2;
tbdelst=tbdelcon.createStatement();
tbdelrs=tbdelst.executeQuery("select * from "+pm.selected);
tbdelrsmd=tbdelrs.getMetaData();
colcount=tbdelrsmd.getColumnCount();
int loc=35;
fields=new JCheckBox[colcount];
for(int i=1;i<=colcount;i++)
{

fields[i-1]=new JCheckBox(tbdelrsmd.getColumnLabel(i));
fields[i-1].setBounds(50,loc,120,20);
loc=loc+30;
upperpanel.add(fields[i-1]);
}

}catch(Exception tbdelex){ }
container.add(jsp);



}
public void itemStateChanged(ItemEvent event1)
{
if(conditionbutton.isSelected())
condition.setEnabled(true);
else
condition.setEnabled(false);
}
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand().equals("CANCEL"))
dispose();
else
{
String str="delete from " +pm.selected;
if(!condition.getText().equals(""))
str=str+" where "+condition.getText();
System.out.println("del srting"+str);
try
{
tbdelst.executeUpdate(str);
System.out.println("Deletion Completed");
}catch(Exception delex)

{
String m=delex.getMessage();
int len=m.length();
m=m.substring(0,len/4-1)+"\n"+m.substring(len/4-1,len/2-1)+"\n"+m.substring(len/2-1,len*3/4-1)+"\n"+m.substring(len*3/4-1,len-1);
JOptionPane.showMessageDialog(this,m);
}

}
}

}



//Table Insertion
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
class TableInsertion extends JDialog implements ActionListener
{ Container container;
JPanel panel;
JButton ok;
ResultSet rsins;
ResultSet rsindex;
ResultSetMetaData rsmdins;
Statement st;
JTextField text[];
JLabel lab[],head1, head2,tab;
JLabel lab2[];
String type[];
int col;
int count=0;
int labloc=82;
int txtloc=82;
ProjectMain pm;

TableInsertion(ProjectMain pm)
{
super(pm,true);
setTitle("INSERTION SECTION");
Appearance ap=new Appearance();
this.pm=pm;
container=getContentPane();
panel=new JPanel();
panel.setBackground(ap.col2);
panel.setLayout(null);
JButton ok=new JButton("O.K");
ok.setBackground(Color.blue);
ok.setForeground(Color.white);
ok.setBorder(ap.b1);
ok.setFont(ap.f2);
head1=new JLabel(" TYPE");
head1.setForeground(Color.white);
head1.setBackground(ap.col2);
head1.setFont(ap.f4);
head1.setBorder(ap.b1);
head2=new JLabel(" FIELD");
head2.setFont(ap.f4);
head2.setBorder(ap.b1);
head2.setForeground(Color.white);
head2.setBackground(ap.col2);

try {
st=pm.cnn2.createStatement();
rsins=st.executeQuery("select * from "+pm.selected);

rsmdins=rsins.getMetaData();
col=rsmdins.getColumnCount();


} catch(Exception extab) {System.out.println("Exceeption"+extab);/*extab.printStackTrace();*/}
text=new JTextField[col];
tab=new JLabel(" ENTER VALUES INTO "+pm.selected.toUpperCase());
tab.setFont(ap.f2);
tab.setBorder(ap.b1);
tab.setForeground(Color.white);
tab.setBackground(ap.col2);
lab=new JLabel[col];
lab2=new JLabel[col];
type=new String[col];
for (int i=1;i<=col;i++ )
{
try{
lab[i-1]=new JLabel(" "+rsmdins.getColumnLabel(i));
lab[i-1].setFont(ap.f3);
lab[i-1].setBorder(ap.b1);
lab[i-1].setForeground(Color.white);
lab[i-1].setBackground(ap.col2);
lab2[i-1]=new JLabel(" "+rsmdins.getColumnTypeName(i));//rsmdins.getColumnTypeName(i));
lab2[i-1].setFont(ap.f3);
lab2[i-1].setBorder(ap.b1);
lab2[i-1].setForeground(Color.white);
lab2[i-1].setBackground(ap.col2);
type[i-1]=rsmdins.getColumnTypeName(i);
tab.setBounds(310,15,250,20);
head1.setBounds(249,56,70,20);
head2.setBounds(336,56,70,20);
lab[i-1].setBounds(338,labloc,70,20);
lab2[i-1].setBounds(250,labloc,70,20);
labloc=labloc+35;
text[i-1]=new JTextField();
text[i-1].setFont(ap.f3);
text[i-1].setBorder(ap.b2);
//text[i-1].setForeground(Color.white);
text[i-1].setBackground(Color.white);
text[i-1].setBounds(412,txtloc,110,22);
txtloc=txtloc+35;
panel.add(tab);
panel.add(head1);
panel.add(head2);
panel.add(lab2[i-1]);
panel.add(lab[i-1]);
panel.add(text[i-1]);
text[i-1].addActionListener(this);
}catch(Exception exc){ System.out.println("Exception"+exc);/*exc.printStackTrace();*/}
}
ok.setBounds(337,txtloc,90,20);
panel.add(ok);
ok.addActionListener(this);
container.add(panel);
}
public void actionPerformed(ActionEvent e)
{
String s1=new String();
String s2=new String();

s1=s2="(";
if (e.getActionCommand()=="O.K")
{
dispose();
for(int l=0;l {
if (text[l].getText().equals("")==false)
{ count++;
if(count>1)
{
s1=s1+",";
s2=s2+",";
}
if(type[l].equals("char")||type[l].equals("varchar"))
{
s1=s1+lab[l].getText();
s2=s2+"' "+text[l].getText()+"'";
}
else
{
s1=s1+lab[l].getText();
s2=s2+text[l].getText();

}
}

}
s1=s1+")";
s2=s2+")";

try{
st.executeUpdate("insert into "+pm.selected+" "+s1+" values "+s2);
}catch(SQLException t)
{
String m=t.getMessage();
int len=m.length();
m=m.substring(0,len/4-1)+"\n"+m.substring(len/4-1,len/2-1)+"\n"+m.substring(len/2-1,len*3/4-1)+"\n"+m.substring(len*3/4-1,len-1);
JOptionPane.showMessageDialog(this,m);
}

}
}

}



//Table Selection
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.event.*;
class TableSelection extends JDialog implements ItemListener, ActionListener
{
Container container;
JPanel panel1,panel2;
Connection tbselcon;
Statement tbselst;
ResultSet tbselrs;
ResultSetMetaData tbselrsmd;
JCheckBox allfields,distinct;
JCheckBox[] fieldtext;
JLabel conditionlb,fieldlb,distinctlb;
JRadioButton sum,avg,count1,count2,min,max,group,orderby;
JSplitPane jsp1,jsp2,jsp3,jsp4,jsp5,jsp6,jsp7,jsp8;
JPanel leftpanel,rightpanel1,rightpanel2,rightpanel3,rightpanel4;
ProjectMain pm;
JButton ok,cancel;
String sel;
int colcount,selcount,function;
JTextField condition,distinctfield,grouptext,ordertext;
JTextField[] text;
TableSelection(ProjectMain pm)
{
super(pm,true);
setTitle("TABLE SELECTION");
this.pm=pm;
container=getContentPane();

leftpanel=new JPanel();
leftpanel.setLayout(null);
rightpanel1=new JPanel();
rightpanel1.setLayout(null);
rightpanel2=new JPanel();
rightpanel2.setLayout(null);
rightpanel3=new JPanel();
rightpanel3.setLayout(null);
rightpanel4=new JPanel();
rightpanel4.setLayout(null);
distinct=new JCheckBox("SELECT DISTINCT FIELDS");
distinct.setBounds(40,20,200,20);
distinct.addItemListener(this);
rightpanel1.add(distinct);
distinctlb=new JLabel("Enter which field to be Distinct");
distinctlb.setBounds(40,45,250,30);
rightpanel1.add(distinctlb);
distinctfield=new JTextField();
distinctfield.setBounds(40,70,200,30);
//distinctfield.addItemListener(this);
rightpanel1.add(distinctfield);
ok=new JButton("O.K");
ok.setBounds(10,400,70,20);
ok.addActionListener(this);
cancel=new JButton("CANCEL");
cancel.setBounds(80,400,80,20);
cancel.addActionListener(this);
leftpanel.add(cancel);
leftpanel.add(ok);

try
{
tbselcon=pm.cnn2;
tbselst=tbselcon.createStatement();
tbselrs=tbselst.executeQuery("select * from "+pm.selected);
tbselrsmd=tbselrs.getMetaData();
colcount=tbselrsmd.getColumnCount();
int loc=40;
fieldtext=new JCheckBox[colcount];
for(int i=1;i<=colcount;i++)
{

fieldtext[i-1]=new JCheckBox(tbselrsmd.getColumnLabel(i));
fieldtext[i-1].setBounds(40,loc,120,20);
fieldtext[i-1].addItemListener(this);
loc=loc+40;
leftpanel.add(fieldtext[i-1]);
}
leftpanel.setEnabled(false);
}catch(Exception altex){System.out.println("cannot connect to the table");}
/*jsp1=new JSplitPane(JSplitPane.VERTICAL_SPLIT,leftpanel,rightpanel1);
jsp1.setDividerLocation(150);
jsp2=new JSplitPane(JSplitPane.VERTICAL_SPLIT,rightpanel1,rightpanel2);
jsp2.setDividerLocation(150);
jsp3=new JSplitPane(JSplitPane.VERTICAL_SPLIT,rightpanel2,rightpanel3);
jsp3.setDividerLocation(150);
jsp4=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jsp2,jsp3);
jsp4.setDividerLocation(150);
jsp5=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jsp1,jsp4);
jsp5.setDividerLocation(150);
jsp6=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jsp5,rightpanel4);
jsp6.setDividerLocation(150);*/
//container.add(jsp5);
jsp1=new JSplitPane(JSplitPane.VERTICAL_SPLIT,leftpanel,rightpanel1);
jsp1.setDividerLocation(170);
jsp1.setDividerSize(2);
jsp1.resetToPreferredSizes();
jsp2=new JSplitPane(JSplitPane.VERTICAL_SPLIT,rightpanel1,rightpanel2);
jsp2.setDividerLocation(20);
jsp2.setDividerSize(2);
jsp2.resetToPreferredSizes();
jsp3=new JSplitPane(JSplitPane.VERTICAL_SPLIT,rightpanel2,rightpanel3);
jsp3.setDividerLocation(40);
jsp3.setDividerSize(2);
jsp3.resetToPreferredSizes();
jsp4=new JSplitPane(JSplitPane.VERTICAL_SPLIT,rightpanel3,rightpanel4);
jsp4.setDividerLocation(150);
jsp4.setDividerSize(2);
jsp4.resetToPreferredSizes();
jsp5=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jsp2,jsp3);
jsp5.setDividerLocation(80);
jsp5.setDividerSize(2);
jsp5.resetToPreferredSizes();
jsp6=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jsp3,jsp4);
jsp6.setDividerLocation(100);

jsp6.setDividerSize(2);
jsp6.resetToPreferredSizes();
jsp7=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jsp5,jsp6);
jsp7.setDividerLocation(120);
jsp7.setDividerSize(2);
jsp7.resetToPreferredSizes();
jsp8=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jsp1,jsp7);
jsp8.setDividerLocation(175);
jsp8.resetToPreferredSizes();

jsp8.setDividerSize(2);
container.add(jsp8);
allfields=new JCheckBox("Select All fields");
allfields.setBounds(40,200,200,30);
allfields.addItemListener(this);
leftpanel.add(allfields);
conditionlb=new JLabel("SPECIFY CONDITION FOR SELECTION IF ANY");
conditionlb.setBounds(20,20,250,30);
rightpanel2.add(conditionlb);
condition=new JTextField();
condition.setBounds(20,50,250,30);
rightpanel2.add(condition);
orderby=new JRadioButton("ORDER BY");
orderby.setBounds(280,45,100,20);
orderby.setEnabled(false);
ordertext=new JTextField();
ordertext.setBounds(365,45,80,20);
ordertext.setEnabled(false);
// orderby.addItemListener(this);
rightpanel2.add(orderby);
rightpanel2.add(ordertext);
sum=new JRadioButton("SUM");
sum.setBounds(30,20,90,20);
sum.addItemListener(this);
avg=new JRadioButton("AVERAGE");
avg.setBounds(30,40,90,20);
avg.addItemListener(this);
count1=new JRadioButton("COUNT");
count1.setBounds(30,60,90,20);
count1.addItemListener(this);
count2=new JRadioButton("COUNT*");
count2.setBounds(30,80,90,20);
count2.addItemListener(this);
min=new JRadioButton("MINIMUM");
min.setBounds(30,100,90,20);
min.addItemListener(this);
max=new JRadioButton("MAXIMUM");
max.addItemListener(this);
max.setBounds(30,120,90,20);
text=new JTextField[6];
int loc2=20;
for(int i=0;i<6;i++)
{
text[i]=new JTextField();

text[i].setBounds(120,loc2,90,20);
text[i].setEnabled(false);
rightpanel3.add(text[i]);
loc2=loc2+20;
}
group=new JRadioButton("GROUP BY");
group.setBounds(220,20,90,20);
rightpanel3.add(group);
grouptext=new JTextField();
grouptext.setBounds(310,20,90,20);
grouptext.setEnabled(false);
rightpanel3.add(grouptext);
rightpanel3.add(sum);
rightpanel3.add(avg);
rightpanel3.add(count1);
rightpanel3.add(count2);
rightpanel3.add(min);
rightpanel3.add(max);


}
public void itemStateChanged(ItemEvent e2)
{
if(allfields.isSelected())
{
for(int i=0;i {
fieldtext[i].setEnabled(false);
}
sum.setEnabled(false);

avg.setEnabled(false);
count1.setEnabled(false);
count2.setEnabled(false);
min.setEnabled(false);
max.setEnabled(false);
group.setEnabled(false);
grouptext.setEnabled(false);
orderby.setEnabled(true);
ordertext.setEnabled(true);
}
for(int i=0;i {
if (fieldtext[i].isSelected())
{
allfields.setEnabled(false);
sum.setEnabled(false);
avg.setEnabled(false);
count1.setEnabled(false);
count2.setEnabled(false);
min.setEnabled(false);
max.setEnabled(false);
group.setEnabled(false);
grouptext.setEnabled(false);
orderby.setEnabled(true);
ordertext.setEnabled(true);
}
}
if(sum.isSelected()||avg.isSelected()||count1.isSelected()||count2.isSelected()||min.isSelected()||max.isSelected())
{
for(int i=0;i {
fieldtext[i].setEnabled(false);
}
allfields.setEnabled(false);
distinct.setEnabled(false);
orderby.setEnabled(false);
ordertext.setEnabled(false);
}
if(sum.isSelected())
text[0].setEnabled(true);
grouptext.setEnabled(true);
if(avg.isSelected())
text[1].setEnabled(true);
grouptext.setEnabled(true);
if (count1.isSelected())
text[2].setEnabled(true);
grouptext.setEnabled(true);
if(count2.isSelected())
text[3].setEnabled(true);
grouptext.setEnabled(true);
if(min.isSelected())
text[4].setEnabled(true);
grouptext.setEnabled(true);
if(max.isSelected())
text[5].setEnabled(true);
grouptext.setEnabled(true);


}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("CANCEL"))
dispose();
else{
selcount=0;
function = 0;
sel="select ";
if(distinct.isSelected() && !distinctfield.getText().equals(""))
sel=sel+" Distinct ";
if(!distinctfield.getText().equals(""))
sel=sel+distinctfield.getText();
if(allfields.isSelected())
sel=sel+" * ";
for(int i=0;i {
if(fieldtext[i].isSelected())
{

selcount++;
if (selcount>1)
sel=sel+",";
sel=sel+fieldtext[i].getText();
}

}

if(sum.isSelected() &&!text[0].getText().equals(""))
{
function++;
if(function>1)
sel=sel+",";
sel=sel+"sum("+text[0].getText()+") as s";
}
if(avg.isSelected() &&!text[1].getText().equals(""))
{
function++;
if(function>1)
sel=sel+",";
sel=sel+"avg("+text[1].getText()+") as a";
}
if(count1.isSelected() &&!text[2].getText().equals(""))
{
function++;
if(function>1)
sel=sel+",";
sel=sel+"count("+text[2].getText()+")";
}
if(count2.isSelected() )
{
function++;
if(function>1)
sel=sel+",";
sel=sel+"count(*)";
}
if(min.isSelected() &&!text[4].getText().equals(""))
{
function++;
if(function>1)
sel=sel+",";
sel=sel+" min("+text[4].getText()+")";
}
if(max.isSelected() &&!text[5].getText().equals(""))
{
function++;
if(function>1)
sel=sel+",";
sel=sel+" max("+text[5].getText()+")";
}
if(group.isSelected() &&!grouptext.getText().equals(""))
{
function++;
if(function>1)
sel=sel+",";
sel=sel+" group by "+grouptext.getText();
}
sel=sel+" from "+pm.selected;
if(orderby.isSelected()&&!ordertext.equals(""))
sel=sel+" "+"order by "+ordertext.getText();
if(!condition.getText().equals(""))
sel=sel+" "+condition.getText();
System.out.println("string"+sel);
showTable();


}
}


public void showTable()
{


int l=0;
int k=0;

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection tbselcon=DriverManager.getConnection("jdbc:odbc:;Server="+pm.Info.info.get("Server")+";Driver=SQL Server;database="+pm.obj3.toString()+";user="+pm.Info.info.get("User"));
Statement st=tbselcon.createStatement();
ResultSet rs=st.executeQuery(sel);
ResultSetMetaData rsmd=rs.getMetaData();
int col=rsmd.getColumnCount();

while(rs.next())
{
l++;
}
String head[]=new String[col];
String data[][]=new String[l][col];
for(k=0;k {
head[k]=rsmd.getColumnLabel(k+1);

}


int row=0;
rs=st.executeQuery(sel);

while(rs.next())
{
for(k=0;k {
data[row][k]=rs.getString(k+1);
}
row++;
}

JTable table=new JTable(data,head);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scroll=new JScrollPane(table,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

/* rightpanel1.removeAll();
rightpanel1.setLayout(new BorderLayout());
rightpanel1.add(scroll,BorderLayout.CENTER);
rightpanel1.validate();*/
//rightpanel4.removeAll();
rightpanel4.setLayout(new BorderLayout());
rightpanel4.add(scroll,BorderLayout.NORTH);
rightpanel4.validate();

System.out.println("executed");
}catch(Exception ex)
{
String m=ex.getMessage();
int len=m.length();
m=m.substring(0,len/4-1)+"\n"+m.substring(len/4-1,len/2-1)+"\n"+m.substring(len/2-1,len*3/4-1)+"\n"+m.substring(len*3/4-1,len-1);
JOptionPane.showMessageDialog(this,m);
for(int i=0;i {
fieldtext[i].setSelected(false);
}
}

}
}



//Table Updation
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
class TableUpdation extends JDialog implements ItemListener ,ActionListener
{
Container container;
JPanel rightpanel,leftpanel;
JRadioButton[] fields;
JRadioButton conditionlb;
JButton ok,cancel;
JLabel fieldlb,newvaluelb;
JTextField condition;
JTextField[] newvalue;
ProjectMain pm;
Connection tbupcon;
Statement tbupst;
ResultSet tbuprs;
ResultSetMetaData tbuprsmd;
int colcount;
TableUpdation(ProjectMain pm)
{
super(pm,true);
setTitle("TABLE UPDATION");
this.pm=pm;
container=getContentPane();
rightpanel=new JPanel();
leftpanel=new JPanel();

ok=new JButton("O.K");
cancel=new JButton("CANCEL");
fieldlb=new JLabel("SELECT FIELDS TO BE UPDATED");
fieldlb.setBounds(125,5,220,20);
newvaluelb=new JLabel("new value of the field");
newvaluelb.setBounds(300,5,220,20);
conditionlb=new JRadioButton("SPECIFY CONDITION IF ANY FOR UPDATION");
conditionlb.addItemListener(this);
JScrollPane scroll=new JScrollPane(rightpanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JSplitPane jsp=new JSplitPane(JSplitPane.VERTICAL_SPLIT,scroll,leftpanel);
jsp.setDividerLocation(120);
condition=new JTextField();
condition.setEnabled(false);
int loc=20;
rightpanel.setLayout(null);
rightpanel.add(fieldlb);
leftpanel.setLayout(null);
leftpanel.add(ok);
leftpanel.add(cancel);
leftpanel.add(conditionlb);
leftpanel.add(condition);
ok.setBounds(130,90,80,20);
cancel.setBounds(210,90,80,20);
ok.addActionListener(this);
cancel.addActionListener(this);

try
{

tbupcon=pm.cnn2;
tbupst=tbupcon.createStatement();
tbuprs=tbupst.executeQuery("select * from "+pm.selected);
tbuprsmd=tbuprs.getMetaData();
colcount=tbuprsmd.getColumnCount();
fields=new JRadioButton[colcount];
newvalue=new JTextField[colcount];
for(int i=1;i<=colcount;i++)
{

fields[i-1]=new JRadioButton(tbuprsmd.getColumnLabel(i));
fields[i-1].setBounds(100,loc,80,20);
newvalue[i-1]=new JTextField();
newvalue[i-1].setBounds(190,loc,80,20);
newvalue[i-1].setEnabled(false);
fields[i-1].setBackground(Color.white);
fields[i-1].addItemListener(this);
loc=loc+30;
rightpanel.add(fields[i-1]);
rightpanel.add(newvalue[i-1]);
}


}catch(Exception ex){System.out.println("exception"+ex);}
conditionlb.setBounds(80,20,270,20);
condition.setBounds(90,40,250,30);

container.add(jsp);
}
public void itemStateChanged(ItemEvent event1)
{
if(conditionlb.isSelected())
{
condition.setEnabled(true);
condition.requestFocus();
}
for(int i=0;i {
if(fields[i].isSelected())
newvalue[i].setEnabled(true);
newvalue[i].requestFocus();

}
}

public void actionPerformed(ActionEvent event2)
{
if(event2.getActionCommand().equals("CANCEL"))
dispose();
else{
String str1=new String();
String str2=new String();
String str3=new String();
str1="update "+pm.selected+" set ";
int upcount=0;
for(int i=0;i {

if(fields[i].isSelected() && !(newvalue[i].getText().equals("")))
{

upcount++;
System.out.println("upcount"+upcount);
if (upcount>1)
str3=str3+",";
try
{
str2=tbuprsmd.getColumnTypeName(i+1);
System.out.println(str2);
}catch(Exception ex2){System.out.println("exception in typename");}
if(str2.equals("varchar")||str2.equals("nvarchar")||str2.equals("char")||str2.equals("nchar"))
str3=str3+fields[i].getText()+"= '"+newvalue[i].getText().trim()+"'";
else
str3=str3+fields[i].getText()+"="+newvalue[i].getText().trim();
str1=str1+str3;
}
}// for
if(!condition.getText().equals(""))
str1=str1+" where "+condition.getText().trim();
System.out.println("string "+str1);
try
{
tbupst.executeUpdate(str1);
System.out.println(pm.selected+"Updated ");
}catch(Exception ex3)
{
String m=ex3.getMessage();
int len=m.length();
m=m.substring(0,len/4-1)+"\n"+m.substring(len/4-1,len/2-1)+"\n"+m.substring(len/2-1,len*3/4-1)+"\n"+m.substring(len*3/4-1,len-1);
JOptionPane.showMessageDialog(this,m);

}
}
}
}




1 comment:

Anonymous said...

How do i implement this? why are there two classes with the same name?