Results 1 to 1 of 1
  1. #1
    Join Date
    May 2004
    Posts
    1

    Unanswered: Insert record into mySQL table

    I'm having a little trouble inserting a record into a mySQL table. I'm using a Java Bean to do the insert.

    insert.jsp
    <form name="insertMovie" method="post" action="insertNow.jsp">

    insertNow.jsp
    <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
    <jsp:useBean id="movieBean" class="beans.movieBean" scope="request"/>

    <%
    try
    {
    /*SET THE REQUEST OBJECT */
    movieBean.setRequest(request);

    /* INSERT MOVIE INTO DATABASE */
    movieBean.insertMovie();
    }
    catch (Exception e)
    {
    System.out.println(e.getMessage());
    }
    %>


    movieBean.javapackage beans;
    import java.lang.*;
    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class movieBean {
    String mTitle;
    String mRating;
    String mGenre;
    String mDuration;
    String mTime1;
    String mTime2;
    String mTime3;
    String mTime4;
    String mTime5;
    String mTime6;
    String mDesc;
    String mNowShow;
    String mActor;
    String mDirector;
    String mProducer;
    String mWriter;
    String mStudio;
    HttpServletRequest request = null;
    Connection dbConn = null;

    //SETS THE REQUEST OBJECT. THIS IS USED FOR GETTING PARAMETERS
    public void setRequest(HttpServletRequest request){
    this.request = request;
    }

    //CONNECT TO THE DATABASE
    public void connectToDatabase()
    throws Exception
    {
    Class.forName("org.gjt.mm.mysql.Driver");
    String host="localhost";
    String user="";
    String pass="";
    String db="dbname";
    String connString;
    connString = "jdbc:mysql://" + host + "/" + db + "?user=" + user + "&password=" + pass;
    //connString = "jdbc:mysql://" + host + "/" + db;
    Connection dbConn = DriverManager.getConnection(connString);
    if(dbConn== null)
    {
    throw new Exception("The database connection failed");
    }
    }

    //INSERT A RECORD INTO THE DATABASE - tblmovie
    public void insertMovie()
    throws Exception
    {
    //Connects to the database
    connectToDatabase();

    //Get the parameters from the insertMovie.jsp form (form values)
    mTitle = request.getParameter("Title");
    mRating = request.getParameter("Rating");
    mGenre = request.getParameter("Genre");
    mDuration = request.getParameter("Duration");
    mTime1 = request.getParameter("Time1");
    mTime2 = request.getParameter("Time2");
    mTime3 = request.getParameter("Time3");
    mTime4 = request.getParameter("Time4");
    mTime5 = request.getParameter("Time5");
    mTime6 = request.getParameter("Time6");
    mDesc = request.getParameter("Desc");
    mNowShow = request.getParameter("Showing");
    mActor = request.getParameter("Actor");
    mDirector = request.getParameter("Director");
    mProducer = request.getParameter("Producer");
    mWriter = request.getParameter("Writer");
    mStudio = request.getParameter("Studio");

    //Statement stmt = dbConn.createStatement();

    //BUILD THE SQL INSERT STATEMENT
    String sql = "INSERT INTO tblmovie " +
    "(Title, Rating, Genre, Duration, Time1, Time2, Time3, Time4, Time5, Time6" +
    "Description, Showing, Actor, Director, Producer, Writer, Studio)" +
    "VALUES " +
    "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    //THIS PREPARES THE SELECT STATEMENT
    PreparedStatement statement = dbConn.prepareStatement(sql);

    //SET THE PARAMETERS and RUN IT
    statement.setObject (1, mTitle);
    statement.setObject (2, mRating);
    statement.setObject (3, mGenre);
    statement.setObject (4, mDuration);
    statement.setObject (5, mTime1);
    statement.setObject (6, mTime2);
    statement.setObject (7, mTime3);
    statement.setObject (8, mTime4);
    statement.setObject (9, mTime5);
    statement.setObject (10, mTime6);
    statement.setObject (11, mDesc);
    statement.setObject (12, mNowShow);
    statement.setObject (13, mActor);
    statement.setObject (14, mDirector);
    statement.setObject (15, mProducer);
    statement.setObject (16, mWriter);
    statement.setObject (17, mStudio);

    statement.executeUpdate();

    //CLOSE THE DATABASE CONNECTION
    dbConn.close();

    }
    ....
    }



    Why isn't this inserting the record? Please help, I've been struggling with this for a couple of days now.

    Thanks
    Last edited by timtm; 05-25-04 at 12:14.

Posting Permissions

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