Write a JDBC application using swing for the following:
Accept Query in TextField For
1. Create Table
2. Alter Table
3. Drop Table
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
class DDL extends JFrame implements ActionListener
{
JLabel l1;
JButton b1,b2,b3;
JTextArea t1;
DDL()
{
setLayout(null);
l1=new JLabel("Type DDL Query");
b1=new JButton("Create Table");
b2=new JButton("Alter Table");
b3=new JButton("Drop Table");
t1=new JTextArea();
l1.setBounds(20,30,100,20);
t1.setBounds(150,30,250,150);
b1.setBounds(10,250,120,20);
b2.setBounds(130,250,120,20);
b3.setBounds(250,250,120,20);
add(l1);
add(t1);
add(b1);
add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
setSize(440,350);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
Statement st=cn.createStatement();
String str=t1.getText();
st.executeUpdate(str);
JOptionPane.showMessageDialog(null,"Table created");
cn.close();
}catch(Exception e)
{
System.out.println(e);
}
}
if(ae.getSource()==b2)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
Statement st=cn.createStatement();
String str1=t1.getText();
st.executeUpdate(str1);
JOptionPane.showMessageDialog(null,"Table altered");
cn.close();
}catch(Exception e)
{
System.out.println(e);
}
}
if(ae.getSource()==b3)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
Statement st=cn.createStatement();
String str1=t1.getText();
st.executeUpdate(str1);
JOptionPane.showMessageDialog(null,"Table droped");
cn.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main(String args[])
{
new DDL().show();
}
}
1. Create Table
2. Alter Table
3. Drop Table
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
class DDL extends JFrame implements ActionListener
{
JLabel l1;
JButton b1,b2,b3;
JTextArea t1;
DDL()
{
setLayout(null);
l1=new JLabel("Type DDL Query");
b1=new JButton("Create Table");
b2=new JButton("Alter Table");
b3=new JButton("Drop Table");
t1=new JTextArea();
l1.setBounds(20,30,100,20);
t1.setBounds(150,30,250,150);
b1.setBounds(10,250,120,20);
b2.setBounds(130,250,120,20);
b3.setBounds(250,250,120,20);
add(l1);
add(t1);
add(b1);
add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
setSize(440,350);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
Statement st=cn.createStatement();
String str=t1.getText();
st.executeUpdate(str);
JOptionPane.showMessageDialog(null,"Table created");
cn.close();
}catch(Exception e)
{
System.out.println(e);
}
}
if(ae.getSource()==b2)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
Statement st=cn.createStatement();
String str1=t1.getText();
st.executeUpdate(str1);
JOptionPane.showMessageDialog(null,"Table altered");
cn.close();
}catch(Exception e)
{
System.out.println(e);
}
}
if(ae.getSource()==b3)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
Statement st=cn.createStatement();
String str1=t1.getText();
st.executeUpdate(str1);
JOptionPane.showMessageDialog(null,"Table droped");
cn.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main(String args[])
{
new DDL().show();
}
}
Comments
Post a Comment