• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

connection to dabase & Insert statement in JSP

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
fathimasulthan
Asked:
fathimasulthan
  • 3
  • 3
  • 2
1 Solution
 
bkrahmerCommented:
What problem are you having?  SQL syntax?  Java exception?
brian
0
 
santedespritCommented:
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
 
santedespritCommented:
forgot to mention that:

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

the srsdb is the database name so change it accordingly
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
bkrahmerCommented:
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
 
santedespritCommented:
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
 
bkrahmerCommented:
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
 
fathimasulthanAuthor Commented:
HI santedesprit ,
THANKS FOR UR HELP

THANKS for one and all who replied.

FAthima
0
 
fathimasulthanAuthor Commented:
THANKS for Ur reply
Thanks
Fathima
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now