?
Solved

connection to dabase & Insert statement in JSP

Posted on 2003-02-27
8
Medium Priority
?
299 Views
Last Modified: 2010-03-31
Hi all,
  I am using MySQL & creatng JSP pages.
  Can anybody ell me how to excute the Insert statement? If U give me the code snippet , it ll be more useful..

thanks
Fathima
0
Comment
Question by:fathimasulthan
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8039436
What problem are you having?  SQL syntax?  Java exception?
brian
0
 

Accepted Solution

by:
santedesprit earned 200 total points
ID: 8039982
Connection
==========
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/srsdb";
Connection c = DriverManager.getConnection( url );
} catch (ClassNotFoundException e) {
return "Could not find the database driver";
} catch (SQLException e) {
return "Could not connect to the database";
} catch (Exception e) {
return "General Exception";
}
SQL
===
try{
strSql = "INSERT INTO table_name(table_field) values('value')";
stmt = c.createStatement();
r = stmt.executeQuery(strSql);
               
stmt.close();
c.close();
}catch(SQLException sql){
sql.printStackTrace();
}
0
 

Expert Comment

by:santedesprit
ID: 8040035
forgot to mention that:

String url = "jdbc:mysql://localhost:3306/srsdb";

the srsdb is the database name so change it accordingly
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

 
LVL 2

Expert Comment

by:bkrahmer
ID: 8043370
A cursory examination says that it looks right.  Are you getting an exception?  You should examine 'r' as well, it tells you how many rows were created.
brian
0
 

Expert Comment

by:santedesprit
ID: 8050138
In case of query, strSQL is of String type, stmt is PreparedStatement type, r is of ResultSet type and c is of Connection type
0
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8052644
I don't think you want a PreparedStatement, I think you want a Statement.  In any case, I'm not going to help you any further until you answer all of my questions in depth!  Give up some details man!

brian
0
 

Author Comment

by:fathimasulthan
ID: 8078185
HI santedesprit ,
THANKS FOR UR HELP

THANKS for one and all who replied.

FAthima
0
 

Author Comment

by:fathimasulthan
ID: 8078191
THANKS for Ur reply
Thanks
Fathima
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

770 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