source file name: jdbcgui.java
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
class jdbcgui extends Frame implements ActionListener
{
TextField t1;
public jdbcgui()
{
FlowLayout fl=new FlowLayout();
setLayout(fl);
Label l1=new Label("Enter any SQL statement: ");
t1=new TextField(30);
Button bt=new Button("click");
add(l1);add(t1);add(bt);
bt.addActionListener(this);
}
public void actionPerformed(java.awt.event.ActionEvent ae)
{
String s=t1.getText();
System.out.println("sql is: "+s);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:javadsn");
Statement st = conn.createStatement();
if(s.startsWith("select"))
{
ResultSet rs=st.executeQuery(s);
String data="";
while(rs.next())
{
//System.out.println(rs.getString(1)+" "+rs.getString(2)+ " "+rs.getString(3));
data=data+rs.getString(1)+" "+rs.getString(2)+ " "+rs.getString(3)+"\n";
}
TextArea ta=new TextArea(data);
Dialog d=new Dialog(this);
d.setLocation(400,100);
d.add(ta);
d.setSize(300,300);
d.setVisible(true);
}
else
st.executeUpdate(s);
conn.close();
st.close();
}
catch(SQLException e)
{System.out.println(e); }
catch(Exception e1)
{System.out.println(e1);}
}
public static void main(String[] args)
{
jdbcgui f=new jdbcgui();
f.setSize(300,300);
f.setVisible(true);
}
}
output:
0 Comments
Post a Comment