File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes submitting the applet input details to database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "submitting the applet input details to database" Watch "submitting the applet input details to database" New topic
Author

submitting the applet input details to database

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I am having an applet page which is like an input form with the details name,age,etc...and a submit button.now i have to submit the details which is to be stored in my database.I have written the following code to display the input form in applet and another code is to update the details in database.please help me whether it is correct or not?

code: To display the input form in applet(JAgi.java)

public class JAgi
{
JFrame frame;
JPanel panel;

JLabel labelCardid;
JLabel labelName;

JTextField textCardid;
JTextField textName;
JButton loginButton=new JButton("Enter");

public JAgi()
{
panel=new JPanel();

frame=new JFrame("Customer Details");
frame.setSize(300,300);
frame.setVisible(true);
frame.getContentPane().add(panel);

labelCardid = new JLabel("Card ID:");
labelName = new JLabel("Name:");
textCardid = new JTextField(15);
textName = new JTextField(15);

panel.add(labelCardid);
panel.add(textCardid);
panel.add(labelName);
panel.add(textName);
panel.add(loginButton);
}

public static void main(String args[])
{
new JAgi();
}

}

Code:To get the values and to insert into db(JAgidb.java)

public class JAgidb
{
String Cardid=request.getParameter("textcardid");
String Name=request.getParameter("textName");
try



{

Class.forName("com.mysql.jdbc.Driver").newInstance();

Connection C = DriverManager.getConnection("jdbc:mysql://192.168.1.5:3306/test","dialer","1234");

Statement st = C.createStatement();

ResultSet rs=null;

String query="select * from credit_cards";

rs=st.executeQuery(query);



st.executeUpdate("INSERT INTO `credit_cards` (`cardid` , `name` )VALUES('"+ Cardid +"' , '"+ Name +"')");





}

catch(Exception e)

{}

}

I this code is correct ,how to submit that applet page to this inserting page?whether i have to use onclick event?if so where i have to write?
please help me.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42913
    
  68
First of all, this is never a good idea:
catch(Exception e) {}

At the least you need to log errors, and also notify the user that one occurred.

Since the JAgidb class has a field called "request", I assume it's running in a servlet - is that correct?

"on click" is a JavaScript thing; in Java you need to add an ActionListener to the button. You can then use the java.net.HttpUrlConnection class (or, probably easier, the Jakarta Commons HttpClient library) to make an HTTP request to your servlet.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks for your advice.Actually i put e.printStackTrace() inside the catch.but i have missed that to post.I am not using servlet and i just want to connect the java applet directly to mysql database.I think i was not correct in the place where i am getting the input details.Is the following code correct to get the input and to update the datas to database?

public class JAgi extends JApplet implements ActionListener
{
JFrame frame;
JPanel panel;

JLabel labelCardid;
JLabel labelName;

JTextField textCardid;
JTextField textName;
JButton loginButton=new JButton("Enter");

public JAgi()
{
panel=new JPanel();

frame=new JFrame("Customer Details");
frame.setSize(300,300);
frame.setVisible(true);
frame.getContentPane().add(panel);

labelCardid = new JLabel("Card ID:");
labelName = new JLabel("Name:");
textCardid = new JTextField(15);
textName = new JTextField(15);

panel.add(labelCardid);
panel.add(textCardid);
panel.add(labelName);
panel.add(textName);
panel.add(loginButton);
}

public static void main(String args[])
{
new JAgi();
}
public void actionPerformed(ActionEvent ae)
{

String actionCommand = ae.getActionCommand();
if (ae.getSource() instanceof JButton)
if (actionCommand.equals("ENTER"))
{
String Cardid=textCardid.getText();
String Name = textName.getText();
}
try



{

Class.forName("com.mysql.jdbc.Driver").newInstance();

Connection C = DriverManager.getConnection("jdbc:mysql://192.168.1.5:3306/test","dialer","1234");

Statement st = C.createStatement();

ResultSet rs=null;

String query="select * from credit_cards";

rs=st.executeQuery(query);



st.executeUpdate("INSERT INTO `credit_cards` (`cardid` , `name`)VALUES('"+ Cardid +"' , '"+ Name +"')");





}

catch(Exception e)

{
e.printStackTrace();
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42913
    
  68
It looks like it might work. Are you noticing any problems?

Note that using applets to connect directly to a database is considered bad design, though, and there are also security issues that you need to make sure you understand. What's more, credit card data should never be transmitted over an unencrypted connection, so be sure to use SSL or whatever other secure connection mechanism MySQL offers.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks for your advice.Actually i put e.printStackTrace() inside the catch.but i have missed that to post.I am not using servlet and i just want to connect the java applet directly to mysql database.I think i was not correct in the place where i am getting the input details.Is the following code correct to get the input and to update the datas to database?

public class JAgi extends JApplet implements ActionListener
{
JFrame frame;
JPanel panel;

JLabel labelCardid;
JLabel labelName;

JTextField textCardid;
JTextField textName;
JButton loginButton=new JButton("Enter");

public JAgi()
{
panel=new JPanel();

frame=new JFrame("Customer Details");
frame.setSize(300,300);
frame.setVisible(true);
frame.getContentPane().add(panel);

labelCardid = new JLabel("Card ID:");
labelName = new JLabel("Name:");
textCardid = new JTextField(15);
textName = new JTextField(15);

panel.add(labelCardid);
panel.add(textCardid);
panel.add(labelName);
panel.add(textName);
panel.add(loginButton);
}

public static void main(String args[])
{
new JAgi();
}
public void actionPerformed(ActionEvent ae)
{

String actionCommand = ae.getActionCommand();
if (ae.getSource() instanceof JButton)
if (actionCommand.equals("ENTER"))
{
String Cardid=textCardid.getText();
String Name = textName.getText();
}
try



{

Class.forName("com.mysql.jdbc.Driver").newInstance();

Connection C = DriverManager.getConnection("jdbc:mysql://192.168.1.5:3306/test","dialer","1234");

Statement st = C.createStatement();

ResultSet rs=null;

String query="select * from credit_cards";

rs=st.executeQuery(query);



st.executeUpdate("INSERT INTO `credit_cards` (`cardid` , `name`)VALUES('"+ Cardid +"' , '"+ Name +"')");





}

catch(Exception e)

{
e.printStackTrace();
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42913
    
  68
Is this post different from your previous post?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I think applet is enough for me.coz this project is for working in intranet only.credit card is just an example database.Now i need to update the records which is not filled in input form by filling it.I am getting syntax error.please correct my syntax

st.executeUpdate("UPDATE `credit_cards` SET (`cardid` , `name`)VALUES('"+ Cardid +"')");


when i used the below syntax the details are inserted in a new row.

st.executeUpdate("INSERT INTO `credit_cards` (`cardid` , `name` )VALUES('"+ Cardid +"' , '"+ Name +"')");


but i need to check the datas which are empty throguh java and i have to update the
datas in the existing row.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42913
    
  68
The SQL syntax for updates is

You're checking the validity of the data that you're inserting into the SQL string, right? Because otherwise it's all too easy to be vulnerable to SQL injection attacks.
[ November 30, 2007: Message edited by: Ulf Dittmer ]
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I put the syntax like this:

st.executeUpdate("UPDATE `credit_cards`SET (`cardid`='"+ Cardid +"' , `name`='"+ Name +"')WHERE phone='"+CallerID+"'");


I am getting the following error :

java.sql.SQLException: Syntax error or access violation, message from server: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(`cardid`='23' , `name`='ganesh' , `type`='sse' , `expired` = '2007-11-17' , `ca' at line 1"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1825)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1020)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1109)
at com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:1070)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2027)
at com.mysql.jdbc.Connection.execSQL(Connection.java:1984)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1248)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1194)
at JAgiBackup.actionPerformed(JAgiBackup.java:118)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42913
    
  68
If you post a message in error, you can edit or delete it by clicking the paper-and-pencil icon ().

The syntax you're using doesn't look like the syntax I posted earlier - remove the parentheses and the single quotes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: submitting the applet input details to database