Solved

JSP and MS SQL database (Urgent help needed)!

Posted on 2002-03-07
25
187 Views
Last Modified: 2010-03-31
When i tried to insert my data into the jsp page, i was not unable to insert the data successfully.For example, I tried to paste a whole chunk of articles into a textarea and i got the error "Error: 414 Location: /" or when i tried to insert half the article, i was not able to insert successfully.. My datatype for the database is text.
0
Comment
Question by:Gin_a
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 7
  • 5
25 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 6846687
How do you write your sql statement?
Maybe the text got same special character: " or ' which give you the error.

Maybe you can try to use
   PreparedStatement.setString(1, "");
to insert text.
0
 

Author Comment

by:Gin_a
ID: 6846695
This is my method in my database class

 
     public boolean addNews(String title, int day, int month, int year, String source, String content, String editor, String postedBy, String lastUpdatedBy)
     {
          System.out.println(".."+content+">>");
          int row = 0;
          try
          {
               Statement stmt = con.createStatement();
               String sql = "Insert into News (Title, Day, Month, Year, Source, Content, Editor, Postedby, Lastupdatedby) values('"+title+"',"+day+","+month+","+year+",'"+source+"','"+content+"','"+editor+"','"+postedBy+"','"+lastUpdatedBy+"')";
               row = stmt.executeUpdate(sql);
               if(row == 1)
               {
                    return true;
               }
          }//try
          catch(SQLException e)
          {
               System.out.println(e);
          }
          return false;
     }
"
i have a database class and a servlet class respectively
0
 
LVL 6

Expert Comment

by:kotan
ID: 6846707
Try this,

String sql = "Insert into News(.., content, ...) values(.., ?, ..)";

PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(6, content);
pstmt.executeUpdate();
 

0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:Gin_a
ID: 6846718
sorry,i insert the wrong method
if update.. then how?
just now that method was insert data.. when i update..
public boolean updateNews(News n)
     {
          try
          {
               Statement stmt = con.createStatement();
               String sql = "Update News set Title = '"+n.getTitle()+"', Day = "+n.getDay()+", Month = "+n.getMonth()+", Year = "+n.getYear()+", Source = '"+n.getSource()+"', Content = '"+n.getContent()+"', Editor = '"+n.getEditor()+"', Lastupdatedby = '"+n.getLastUpdatedBy()+"' where NewsID = "+n.getID();
               int row = stmt.executeUpdate(sql);
               if (row == 1)
               {
                    return true;
               }
          }
          catch(SQLException e)
          {
               e.printStackTrace();
          }
          return false;    
     }
0
 
LVL 6

Expert Comment

by:kotan
ID: 6846731
Similar method.

String sql = "Update News set .., Content = ?, ...";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, n.getContent());
pstmt.executeUpdate();
0
 

Author Comment

by:Gin_a
ID: 6846734
sorry,i insert the wrong method
if update.. then how?
just now that method was insert data.. when i update..
public boolean updateNews(News n)
     {
          try
          {
               Statement stmt = con.createStatement();
               String sql = "Update News set Title = '"+n.getTitle()+"', Day = "+n.getDay()+", Month = "+n.getMonth()+", Year = "+n.getYear()+", Source = '"+n.getSource()+"', Content = '"+n.getContent()+"', Editor = '"+n.getEditor()+"', Lastupdatedby = '"+n.getLastUpdatedBy()+"' where NewsID = "+n.getID();
               int row = stmt.executeUpdate(sql);
               if (row == 1)
               {
                    return true;
               }
          }
          catch(SQLException e)
          {
               e.printStackTrace();
          }
          return false;    
     }
0
 
LVL 6

Expert Comment

by:kotan
ID: 6846747
Then,

String sql = "Update News set .., Content = ?, ...";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, n.getContent());
pstmt.executeUpdate();


Does it work?
0
 

Author Comment

by:Gin_a
ID: 6846792
still cannot work.
0
 

Author Comment

by:Gin_a
ID: 6846802
does that mean i have to use another data type. one that allows the inserting of large information.
0
 
LVL 6

Expert Comment

by:kotan
ID: 6846806
Still the same error?
---Error: 414
Where does it come from? db or web server?
0
 
LVL 6

Expert Comment

by:kotan
ID: 6846809
Did you try CLOB datatype.
0
 

Author Comment

by:Gin_a
ID: 6846812
does that mean i have to use another data type. one that allows the inserting of large information.
0
 

Author Comment

by:Gin_a
ID: 6846814
when i lesser the information of my data, i was able to insert.. so i also not sure where the error is from.
0
 

Author Comment

by:Gin_a
ID: 6846846
i use ms sql 2000. there is no clob data type
0
 

Author Comment

by:Gin_a
ID: 6846883
I try to insert 1668 characters with spaces into the textarea and i managed to insert successfully however if more than that, i will not be able to insert into the database. Besides that, when there are ' in my articles, i am not able to insert as well..
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6848032
It sounds like the problem may be with your driver.  Which driver are you using?
0
 

Author Comment

by:Gin_a
ID: 6849006
u mean the database driver?
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6849028
Yes.  Which database driver are you using?
0
 

Author Comment

by:Gin_a
ID: 6849044
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
0
 
LVL 6

Accepted Solution

by:
kotan earned 75 total points
ID: 6849083
I have check the error "Error: 414", It is "Request-URI Too Large" which is throw by web server.

Try to use POST instead of GET

0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6849140
As an expiriment, try using PreparedStatement and setString() to insert the large String instead of one long update statement.  If that doesn't work, try setCharacterStream().  The simple change of binding your variables may make all the difference.  I'll modify your update for you:

PreparedStatement pst = con.prepareStatement("Update News set Title = ?, Day = ?, Month = ?, Year = ?, Source = ?, Content = ?, Editor = ?, Lastupdateby = ? WHERE NewsID = ?");

pst.setString(1, n.getTitle());
pst.setInt(2, n.getDay());
pst.setInt(3, n.getMonth());
pst.setInt(4, n.getYear());
pst.setString(5, n.getSource());
pst.setString(6, n.getContent());
pst.setString(7, n.getEditor());
pst.setString(8, n.getLastUpdatedBy());
pst.setInt(9, n.getID());

int row = stmt.executeUpdate();

If that doesn't work, change the

pst.setString(6, n.getContent());

to:
 Reader rdr = new StringReader(n.getContent());
 pst.setCharacterStream(6, n.getContent(), n.getContent().length());

-----------------------------------------------------

BTW your datatype is text, right?

0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6849147
good catch, kotan.

Gina, the .newInstance() call on your Class.forname is not necessary.
0
 

Author Comment

by:Gin_a
ID: 6849249
i manage to insert the data already. however, if in the information, there are 's in between, i was not able to insert the information into the database. By the way, the data type is text.
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6849280
My method will let you insert the ' characters as is.  That's one of the main advantages to it.
0
 

Author Comment

by:Gin_a
ID: 6849304
I solved all the problems already. Thank for all the help
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

636 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