Question:
how can i insert data into oracle emp table from swing application?
2009-04-01 10:07:44 UTC
i wrote a program to display all ename's of oracle emp table on swing application(in JTextArea by clicking JButton),but i also want to insert ename,empno into emp table of oracle using this swing program.please add remaining code to this program.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

class JButton5 extends JFrame implements ActionListener
{
JButton jb;
JTextArea jt;
Connection con;
Statement st;
ResultSet rs;
Container c;
String str="";
public JButton5()
{
c=getContentPane();
c.setLayout(new FlowLayout());
jb=new JButton("Show emp names");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:mydsn","scott","tiger");
st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=st.executeQuery("select ename from emp");
}

catch(Exception e){;}

c.add(jb);
jb.addActionListener(this);

jt=new JTextArea(15,15);

c.add(jt);

System.out.print("\nAction performed");
}

public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
if(s.trim().equalsIgnoreCase("Show emp names"))
{
try{

while(rs.next())
{
jt.append(rs.getString("ename")+"\n");
//str=str+","+rs.getString("ename");
//jt.setText(str);

//jt.setText(rs.getString("empno"));
//c.setVisible(true);
}
}

catch(Exception e){;}
}
}
public static void main(String args[])
{
JButton5 obj=new JButton5();
obj.setTitle("jlabel");
obj.setSize(1000,1000);
obj.setVisible(true);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Three answers:
venugopal r
2009-04-02 10:44:00 UTC
try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDri...

con=DriverManager.getConnection("jdbc:od...

st=con.createStatement(ResultSet.TYPE_SC...

rs=st.executeQuery("select ename from emp");

}



catch(Exception e){}

========================

put the above code in button action just before

while loop starts.And replace below

remove---rs=st.executeQuery("select ename from emp");

replace with --

st.executeUpdate("insert into emp (ename) values('princemadhu')");

this must be in try{

catch(SQLException){}

========

remove while loop in your program.

recompile and run program.

prince madhu name will be inserted in emp table.

you can add any other columns along with ename in query with comma separation.same number of values must exit in values part.

you will succeed.
uday
2009-04-01 10:21:58 UTC
Hi,



You can use executeUpdate("insert query") to insert data into database.



You have to first form the query before writing the execute update or you can give the query directly in executeUpdate statement.



Ex:



Statement st = conn.createStatement();



st.executeUpdate("insert into emp values (100,'Smith')");
?
2016-05-28 04:04:08 UTC
I think Ray has it right...syntax matters.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...