•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,471 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,272 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 458 | Replies: 1
![]() |
•
•
Join Date: Nov 2006
Posts: 17
Reputation:
Rep Power: 2
Solved Threads: 0
Hello!
I am trying to add new data into a database.
What is the mistake in my code?
I got the error message such as "general error".
I am trying to add new data into a database.
What is the mistake in my code?
I got the error message such as "general error".
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class AddRecord implements ActionListener{
private ScrollingPanel fields;
private JTextArea output;
private Connection connection;
public AddRecord(Connection c, ScrollingPanel f, JTextArea o)
{
connection =c;
fields=f;
output=o;
}
public void actionPerformed( ActionEvent e)
{
try {
Statement statement=connection.createStatement();
if( !fields.last.getText().equals("")&&
!fields.first.getText().equals("")){
String query="INSERT INTO addresses ("+
"firstname, lastname, address, city,"+
"stateorprovince, postalcode, country,"+
"emailaddress, homephone, faxnumber"+
") VALUES ('"+
fields.first.getText()+"','"+
fields.last.getText()+"','"+
fields.address.getText()+"','"+
fields.city.getText()+"','"+
fields.state.getText()+"','"+
fields.zip.getText()+"','"+
fields.country.getText()+"','"+
fields.email.getText()+"','"+
fields.home.getText()+"','"+
fields.fax.getText()+"')";
output.append("\nSending query:"+ connection.nativeSQL(query)+"\n");
int result=statement.executeUpdate(query);
if (result==1)
output.append("\nInsertion successful\n");
else {
output.append("\nInsertion failed\n");
fields.first.setText("");
fields.last.setText("");
fields.address.setText("");
fields.city.setText("");
fields.state.setText("");
fields.zip.setText("");
fields.country.setText("");
fields.email.setText("");
fields.home.setText("");
fields.fax.setText("");
}
}
else
output.append("\nEnter at least first and "+
"last name then press Add\n");
statement.close();
}
catch (SQLException sqlex){
sqlex.printStackTrace();
output.append(sqlex.toString());
}
}
}
Try using a preparedStatement rather than cobbling together a query statement like that.
See the API and Sun tutorials (a link can be found in the API) for more info.
See the API and Sun tutorials (a link can be found in the API) for more info.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- General solution to handle web application errors? (Web Developers' Lounge)
- Microsoft Excel Driver Error (ASP)
- compiling error - help (C++)
- IIS connection will not write data (ASP)
- reading a same input twice from the console (C)
- fstream and struct question (C++)
- ARGH!!! I need help!!! (Windows NT / 2000 / XP / 2003)
- question (C++)
Other Threads in the Java Forum
- Previous Thread: New in Java. Please help!
- Next Thread: Extract columns from text file



Linear Mode