Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

insert data into database

How to create a simple page of inserting data into a database?
0
CruZ3r
Asked:
CruZ3r
1 Solution
 
applekannaCommented:
0
 
CruZ3rAuthor Commented:
err tatz showing/accessing the database in jsp.... i wan to insert data to the database using jsp

thx anyway =)
0
 
applekannaCommented:
That is easy, just follow the guide there
and replace
executeQuery with executeUpdate  like this.

<% Statement statement =
               connection.createStatement();
     ResultSet resultSet =
         statement.executeUpdate("insert into table "); %>


Hope this helps.
DO u need more ...
Cheers1
0
 
yapsingCommented:
get your items like this:
<%
 float netsales        = Float.parseFloat(request.getParameter("netsales"));
 float food            = Float.parseFloat(request.getParameter("food"));

//create your connection here

 PreparedStatement stmt = C.prepareStatement("insert into "+ tab +" (date,netsales,food) values (now(),?,?)");
 stmt.setFloat(1,netsales);
 stmt.setFloat(2,food);
 stmt.executeUpdate();
 response.sendRedirect("wherever u wanna go next");

%>
0
 
nasarudinCommented:
Below is the example to insert data into oracle database.
Hope this could help.

<%@ page import = "java.sql.*"%>
<%@ page import = "oracle.sql.*"%>
<%@ page import = "oracle.jdbc.driver.*"%>
<%
Class.forName("oracle.jdbc.driver.OracleDriver");

Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@myserver.company.com:1521:sidname","scott","tiger");
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

String strDeptNo = request.getParameter("txtDeptNo");
String strDeptName = request.getParameter("txtDeptName");
String strLoc = request.getParameter("txtLoc");

String strSQL = "INSERT INTO DEPT (DEPTNO, DNAME, LOC ) " +
                        "VALUES ( '" + strDeptNo + "', '"  + strDeptName + "', '" + strLoc + "')";

try{
      stmt.executeUpdate(strSQL);
}catch(SQLException SQLEx){
      out.println("Error : " + SQLEx.getMessage());
      if(1==1)return;
}
%>
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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