?
Solved

Write to database

Posted on 2011-10-03
6
Medium Priority
?
329 Views
Last Modified: 2012-05-12
Here's my database table:
Column Name      Data Type  
Artikel                  nvarchar(50)
ID                         int
Namn                   nvarchar(50)
Pris                      float
ISBN                    nvarchar(50)

Why do I get this error?
ALLVARLIG: null
com.microsoft.sqlserver.jdbc.SQLServerException: The name "namn" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
public void Write(int ID, String namn, float pris, String isbn)  {
        try {
            m_Statement = con.createStatement();
        }
        catch (SQLException ex) {
            Logger.getLogger(MysqlConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            m_Statement.executeUpdate("INSERT INTO BokTabell " + "VALUES ('Bok'," +ID +",namn," +pris +",'isbn'");
        }
        catch (SQLException ex) {
            Logger.getLogger(MysqlConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Open in new window

0
Comment
Question by:Mickeys
  • 5
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36904578
namn should be in single quotes
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36904589
INSERT INTO BokTabell " + "VALUES ('Bok'," +ID +",'" + namn + "'," +pris +",'" + isbn+ "'");

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36904598

corrected :
"INSERT INTO BokTabell " + "VALUES ('Bok'," +ID +",'" + namn + "'," +pris +",'" + isbn+ "')");

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Mickeys
ID: 36904607
I wrote "heey" on Namn and get this error now:
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'He'.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36904613
Did you use my latest variant:
"INSERT INTO BokTabell " + "VALUES ('Bok'," +ID +",'" + namn + "'," +pris +",'" + isbn+ "')");

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36904622
Good parctice to do that:
System.out.println("INSERT INTO BokTabell " + "VALUES ('Bok'," +ID +",'" + namn + "'," +pris +",'" + isbn+ "')");
 m_Statement.executeUpdate("INSERT INTO BokTabell " + "VALUES ('Bok'," +ID +",'" + namn + "'," +pris +",'" + isbn+ "')");

Open in new window


and make sure to put System.out.println before executeUpdate and just cuut and paste the string

Thwen if it does not work - you at least will imeediately see the string and spot the error
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 1 hour left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question