Results 1 to 2 of 2
- 09-03-2012, 07:39 PM #1
Java Newbe
- Join Date
- Sep 2012
- Posts
- 1
- Rep Power
- 0
Invoking servlets from a swing class
Hey
Im a java newbe and im making a java swing application which sends data to the servlet.the servlet uses Jdbc to add data to the local database.
I tried HttpURLConnection for ending the url via URL.
The code executes without any error but the data doesnt get inserted.
im attaching the servlet class and the swing class.
In the URL String "http://localhost:8080/christ/myinsdata?name=christu" christ is the name of the folder in the webapps folder,myinsdata is the name of the Url-Pattern in the web.xml file
"the swing class" swingdb.java
"The Servlet class" insdata.javaJava Code:import java.awt.*; import java.sql.*; import java.awt.event.*; import javax.servlet.*; import java.net.*; public class swingdb extends Frame implements ActionListener { TextField t1,t2,t3; Label l1,l2; Button b1,b2; Statement st; Connection cn; String nam,clas; int age; HttpURLConnection connection; public swingdb() { setLayout(new GridLayout(3,3)); t1=new TextField("Ur name"); add(t1); t2=new TextField("Ur age"); add(t2); t3=new TextField("Ur class"); add(t3); l1=new Label("Conn status"); add(l1); l2=new Label("Other status"); add(l2); b1=new Button("Connect"); add(b1); b2=new Button("Save"); add(b2); b1.addActionListener(this); b2.addActionListener(this); setTitle("A swing app"); setSize(300,250); setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { try { nam=t1.getText(); age=Integer.parseInt(t2.getText()); clas=t3.getText(); URL url = new URL( "http://localhost:8080/christ/myinsdata?name=christu" ); connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); l1.setText("Done i think!!"); } catch (Exception e1) { e1.printStackTrace(); } } if(e.getSource()==b2) { dispose(); System.exit(0); } } public static void main(String args[]) { swingdb sd=new swingdb(); } }
Java Code:import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; import java.net.*; public class insdata extends HttpServlet { Connection cn; Statement st; ResultSet rs; String nam,clas; int age,mks; public void doPost(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException { res.setContentType("text/html"); PrintWriter out=res.getWriter(); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn=DriverManager.getConnection("jdbc:odbc:student"); // out.println("Connection Established!!"); nam=req.getParameter("name"); // clas=req.getParameter("clas1"); // age=Integer.parseInt(req.getParameter("age1")); // mks=Integer.parseInt(req.getParameter("mks1")); st=cn.createStatement(); int n=st.executeUpdate("INSERT INTO student(names) VALUES('"+nam+"')"); // out.println("The row is "+n); cn.close(); } catch(SQLException se) { out.println(se); } catch(ClassNotFoundException cnfe) { out.println(cnfe); } } }
pls help!!! thankyou
- 09-04-2012, 09:43 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Invoking servlets from a swing class
Stick some logging in there because at the moment you have no idea if you're getting an exception or not.
Your Swing part is doing nothing with the response (which is where the servlet is sending any exceptions).
I would suggest logging the exceptions on the server as well as handling the response on the client.
Finally, I would also recommend using a PreparedStatement for your query (though this is unlikely to be your current problem).Please do not ask for code as refusal often offends.
Similar Threads
-
Servlets class not teaching servlets
By tsky in forum Java ServletReplies: 5Last Post: 04-26-2011, 10:47 PM -
Reflection Invoking method of "super" class
By Shellback3 in forum Advanced JavaReplies: 2Last Post: 01-31-2011, 04:50 PM -
i need help with a class and swing
By dvd_gonzales in forum Advanced JavaReplies: 2Last Post: 10-25-2009, 11:57 AM -
Accessing one class from another class through swing
By kbyrne in forum AWT / SwingReplies: 5Last Post: 01-03-2008, 07:54 AM -
mapping servlets in user defined xml file and invoking them
By praneeth in forum Advanced JavaReplies: 0Last Post: 07-16-2007, 08:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks