Results 1 to 2 of 2
  1. #1
    analyse300 is offline Java Newbe
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Unhappy 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
    Java 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();
    		
    	}
    }
    "The Servlet class" insdata.java
    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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

    ** This space for rent **

Similar Threads

  1. Servlets class not teaching servlets
    By tsky in forum Java Servlet
    Replies: 5
    Last Post: 04-26-2011, 11:47 PM
  2. Reflection Invoking method of "super" class
    By Shellback3 in forum Advanced Java
    Replies: 2
    Last Post: 01-31-2011, 05:50 PM
  3. i need help with a class and swing
    By dvd_gonzales in forum Advanced Java
    Replies: 2
    Last Post: 10-25-2009, 12:57 PM
  4. Replies: 5
    Last Post: 01-03-2008, 08:54 AM
  5. Replies: 0
    Last Post: 07-16-2007, 09:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •