inserting data into mysql database through java servlet
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: inserting data into mysql database through java servlet

  1. #1
    Join Date
    Jan 2010
    Posts
    161

    inserting data into mysql database through java servlet

    Hi everyone,
    i hope this can be considered the right place even though my situation involves the use of different programs and different programming languages.
    However the main problem is with java connecting to mysql database i guess.

    Basically I am using tomcat and mysql on ubuntu where i do deploy java files and use them as servlet,everthing works i can make web page that access the database and show me the results or search through them but I wanted to insert some data into the table and when i use it ,it say data has been added successfully but when i check no modification has been made.
    this is my code to make it much clear

    [CODE}
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.text.SimpleDateFormat;
    //import java.text.ParseException;
    //import java.util.*;
    import java.sql.*;

    public class InsertStudentDetails extends HttpServlet {
    public void doPost
    (HttpServletRequest request,HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html");

    PrintWriter out = response.getWriter();
    String stName=request.getParameter("stName");
    String stSurName= request.getParameter("stSurName");
    String dCode=request.getParameter("dCode");
    String dob=request.getParameter("dob");


    //Here there will be dob validation which will check if the date format is correct

    if(dob==null)
    {
    out.println("The date of birth format is incorrect");
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    if (dob.trim().length() != dateFormat.toPattern().length())
    {

    out.println("The date of birth format is incorrect");
    }
    //dateFormat.setLenient(false);

    //--------------------------------------------------------------
    //This side does create a connection with the database and inserts the valie captured from the html form

    //setting variables for the sql connection
    Connection conn =null; // Create connection object
    String database = "NAME"; // Name of database
    String user = "NAME"; //
    String password = "PASSWORD";
    String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk/" + database;

    try{
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch(Exception e) {
    System.err.println(e);
    }

    // connecting to database
    try{
    conn = DriverManager.getConnection(url, user, password);

    }
    catch(SQLException se) {
    System.err.println(se);
    }
    // Create select statement and execute it

    //this string does extract the greatest studentno which then will be used to make another studentno
    String lastStNumber="SELECT studentno from students order by studentno desc LIMIT 1";
    int stNumber=0;//=ConvertTo.int32(lastStNumber);
    //string to insert data into the database;
    String insertDataSql="insert into students(stundentno,firstname,degreecode,dob) values(" + 14 +
    ",'" + stName + "'," + stSurName + "," + dCode + ",'" + dob + ";";



    System.err.println("DEBUG: Query: " + lastStNumber);
    Statement stmt;
    try {
    stmt = conn.createStatement();
    //ResultSet rs1 = stmt.executeQuery(lastStNumber);
    //stNumber=rs1.getInt("studentno");
    //this one use the same ResultSet rs1 but executes the sql query which will insert data into the table
    stmt.executeUpdate(insertDataSql);
    conn.close();

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    //stNumber=stmt.executeQuery(lastNumber);//executes a querty in sql to find the greatest studentno
    //stNumber=rs1.getInt("studentno");//assigns the greatest number to stNumber
    //executes the query to insert data in the table

    out.println("<HTML><BODY><H3>The Data has been added Successfully</H3></BODY></HTML>");

    }

    }

    [/CODE]

    I can't understand why it does not insert the data i put in the form which then calls this servlet which then process the data to update the database.

    Thank you in advance

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: inserting data into mysql database through java servlet

    Your code will always show "The Data has been added Successfully" regardless of whether it works or not because you catch the SqlException, dump the stack trace to stdout and then continue as if the sql statement succeeded.

    Your catch clause needs to display a different message and then return.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center