Results 1 to 9 of 9
- 05-18-2011, 01:29 PM #1
Member
- Join Date
- May 2011
- Posts
- 15
- Rep Power
- 0
Need help with derby database connection/insert
Hi guys I'm currently using netbeans and need to make a program for school where the user is able to store questions he's written in a database. I have connected to the database well and have no errors except when I try to save the information to the database here is my code:
String myInsertsql = " VALUES(\'" + newquestion1.getQuestion() + "\', "
+ "\'" + newquestion1.getQuestionNo() + "\', \'" + newquestion1.getAnswerA() + "\', " +
"\'" + newquestion1.getAnswerB() + "\', " + "\'" + newquestion1.getAnswerC() + "\', " + "\'" + newquestion1.getAnswerD() + "\')";
try
{
Connection con = DriverManager.getConnection(databaseURL, "user", "password");
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO QUESTIONS (QUESTION, QUESTIONNUMBER, ANSWER, ANSWERA, ANSWERB, ANSWERC, ANSWERD)" + myInsertsql);
con.close();
} catch (SQLException errorObject)
{
JOptionPane.showMessageDialog(null, "ERROR: "+ errorObject);
}
String databaseURL = "jdbc:derby://localhost:1527/questions";
public main() {
initComponents();
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver" );
}catch (ClassNotFoundException e)
{
JOptionPane.showMessageDialog(null, e);
}
I tried to align it the best I could I get an error saying: Scheme 'USER' does not exist would be awesome if anyone can fix it.
- 05-18-2011, 01:36 PM #2
Unless you put it in [code][/code] tags it will not save the formatting.
What error do you get when you try to save?- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 05-18-2011, 03:56 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Post an SSCCE that demonstrates the problem, along with the full error message as Dark mentioned. Secondly, you might consider using a PreparedStatement
Using Prepared Statements (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics)
- 05-18-2011, 04:26 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 05-18-2011, 04:43 PM #5
Member
- Join Date
- May 2011
- Posts
- 15
- Rep Power
- 0
I'm kind of lost can you link me to some info about schemas? The error code says something about schema and user iI got it on my OP.
EDIT: In properties it said my schema is in APP, which my table is in.Last edited by javaguy345; 05-18-2011 at 04:52 PM.
- 05-18-2011, 05:01 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 05-18-2011, 05:11 PM #7
Member
- Join Date
- May 2011
- Posts
- 15
- Rep Power
- 0
Okay now I'm receiving a different error message it says Columns type of Numeric cannot hold type CHAR. But I'm sure I'm passing an int into the numeric column
- 05-18-2011, 05:43 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-18-2011, 06:10 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
You are surrounding every one of your values in quotes, so essentially saying they're CHARs.
This should be a PreparedStatement, so you wouldn't have to worry about quotes at all. Your SQL would become:
INSERT INTO QUESTIONS (QUESTION, QUESTIONNUMBER, ANSWER, ANSWERA, ANSWERB, ANSWERC, ANSWERD)VALUES(?,?,?,?,?,?,?)
(assuming I've counted my ? correctly)
You would then use the relevant setXXX() method for each value ('?').
Similar Threads
-
Deploy derby database
By Muaz.Sh in forum NetBeansReplies: 11Last Post: 03-17-2011, 06:02 PM -
how to access derby database with servlets
By Mateen in forum Java ServletReplies: 0Last Post: 07-12-2010, 09:16 PM -
Using Derby Database in Netbeans
By DavidG24 in forum New To JavaReplies: 1Last Post: 09-14-2009, 01:29 PM -
How to insert large data into database using one insert query
By sandeepsai39 in forum New To JavaReplies: 3Last Post: 02-28-2009, 09:17 AM -
saving date and time in a derby database from an ejb module
By Dave in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 07-22-2008, 11:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks