#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    3
    Rep Power
    0

    MYSQL connection Problem


    Hi Folks,
    Just wondering if anyone can help me with this problem I'm experiencing. I'm trying to add info to mySQL from a webpage. The JSP thinks it's added the info to the database as I'm getting "Entry added successfully" but in keeps adding NULL instead of the entered info. Hope someone can help. The code I'm using is below I know it's simple but I just can't see where I'm going wrong.
    Thanks,
    Sharon.

    <%@ page language="java" import="java.sql.*" %>

    <%
    // add_res.jsp

    // form data


    String dayId = request.getParameter("day");
    String dateId = request.getParameter("date");
    String startId = request.getParameter("start");
    String endId = request.getParameter("end");
    String hourId = request.getParameter("no_hours");
    String descId = request.getParameter("desc_work");

    // database parameters

    String conn;

    Class.forName("org.gjt.mm.mysql.Driver");

    // pass database parameters to JDBC driver
    Connection Conn =
    DriverManager.getConnection("jdbc:mysql://localhost/timemanage/time_sheet?user=root&password=");

    // query statement
    Statement SQLStatement = Conn.createStatement();

    // generate query
    String Query = "INSERT INTO time_sheet (day) VALUES ('" + dayId + "')";

    // get result code
    int SQLStatus = SQLStatement.executeUpdate(Query);

    if(SQLStatus != 0)
    {
    out.println("Entry succesfully added.");
    }
    else
    {
    out.println("Error! Please try again.");
    }

    // close connection
    SQLStatement.close();
    Conn.close();

    %>


    and this is my html code

    <HTML>
    <HEAD>
    <TITLE>Sample Time Sheet </TITLE>

    </HEAD>

    <BODY>
    <FORM METHOD="get" ACTION="enter2.jsp" ENCTYPE="text/plain" >
    <TABLE Border="0" WIDTH=85%>
    <TR>
    <TD>Day</TD>
    <TR>
    <TD><INPUT TYPE="text" TYPE="dayID" SIZE="8"></TD>
    </tr>
    </TR>
    <TR><TD class="con2" COLSPAN="2"><center><INPUT TYPE="submit" VALUE="submit"></center></TD>

    </TR>
    </TABLE>
    </FORM>
    </BODY>
    </HTML>
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    18
    Rep Power
    0
    Have you checked the SQL-Statement? Does mysql understand the syntax. try it on the mysql-console.

    Another point is the if-statement after the executeUpdate(). it only returns the row count for the statement. so, mysql has inserted one row, but with a null-value.

    cu
  4. #3
  5. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    The reason you are getting the string "null" in your database is because there is no field called "date" being sent from the form. This is what you are trying to get
    Code:
    String dateId = request.getParameter("date");
    And this is what you are sending
    Code:
    <INPUT TYPE="text" TYPE="dayID" SIZE="8">
    Change
    String dateId = request.getParameter("date");
    to
    String dateId = request.getParameter("dayID");
    and see what happens.

    EDIT: I take that back. You also have another error. In this line
    Code:
    <INPUT TYPE="text" TYPE="dayID" SIZE="8">
    You have the TYPE attribute twice. The one with dayID should be NAME like this
    Code:
    <INPUT TYPE="text" NAME="dayID" SIZE="8">
    Make both the changes I suggested and see what happens
    Last edited by Nemi; May 8th, 2003 at 12:51 PM.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    3
    Rep Power
    0

    Thanks!!


    Thanks Nemi for you help I got it working or should I say you go it working!! Trying to teach myself JSP at the moment so it's all new to me!!
    Thanks Again!!
    Sharon

IMN logo majestic logo threadwatch logo seochat tools logo