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

    Join Date
    Mar 2003
    Location
    canterbury
    Posts
    6
    Rep Power
    0

    Unhappy Insert into Database


    Help pls !

    Having problems inserting into database

    When complied passing error

    E:\home\public_html>javac StaffidServlet.java
    StaffidServlet.java:54: cannot resolve symbol
    symbol : method insert (java.lang.String,java.lang.String,java.lang.String)
    location: class Database2
    db.insert(table, colNames, colValues);
    ^
    1 error

    Code:

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;
    import java.util.Date;
    import java.sql.*;
    import java.sql.Timestamp;
    import java.lang.System.*;

    public class StaffidServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    PrintWriter out;

    response.setContentType("text/html");

    out = response.getWriter();

    out.println("<html><body>");

    /* This string parameters will capture information that is received from webpage and pass it through
    the array */


    String [] staffinfo = request.getParameterValues("staff_id");
    String firstStaffinfo = staffinfo [0];

    Database2 db = new Database2();

    String [] staffresults = db.selectWhere("*", "staffmembers", "staff_id= '"+ firstStaffinfo +"'");


    String [] fileinfo = request.getParameterValues("frefcode");
    String firstFileinfo = fileinfo [0];

    String [] fileresults = db.selectWhere("*", "file", "frefcode= '"+ firstFileinfo +"'");

    long currentTime = System.currentTimeMillis();
    Timestamp ts = new Timestamp (currentTime);

    /* Insert into database*/



    String table= "Loan";
    String colNames= "(staff_id, frefcode, dateBorrowed)";
    String colValues= "('" + firstStaffinfo +"'," + firstFileinfo + "," +ts.toString() +")";


    db.insert(table, colNames, colValues);
    out.println(ts);
    out.println("<br>");

    Thanks.
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    It is saying the insert(String, String, String) method is not valid. Post the code for the Database2 object.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    canterbury
    Posts
    6
    Rep Power
    0

    Re. Insert into Database


    I have found out that i have designed my insert method to return an array of
    Strings. It has a return code
    String []
    Therefore when I call it I must put the result into an array of Strings, i have done this and im getting errors- not constructing them properly!


    Database2 object:

    import java.sql.*;

    public class Database2
    {
    private static final String SQL_DRIVER_NAME = "org.postgresql.Driver";
    // The connection to an SQL database.
    private Connection databaseConnection;

    /**
    * Set up a connection to an SQL database.
    * @throws RuntimeException
    * If a database driver cannot be found
    * or if a connection cannot be establised.
    */
    public Database2() throws RuntimeException
    {
    try {
    Class.forName(SQL_DRIVER_NAME);
    databaseConnection =
    DriverManager.getConnection(
    "jdbcostgresql://penguin/as57",
    "as57",
    "pho7do");
    }
    catch(ClassNotFoundException e) {
    throw new RuntimeException("Unable to find the SQL driver: " +
    SQL_DRIVER_NAME);
    }
    catch(SQLException e) {
    reportSQLProblem(e);
    throw new RuntimeException("Unable to establish a connection.");
    }
    }

    /**
    * Run a SELECT FROM query on a particular table.
    * @param what The query pattern.
    * @param table The table on which to run the query.
    */

    public String [] selectFrom(String what, String table) throws RuntimeException
    {
    return generalQuery("SELECT " + what + " FROM " + table);
    }

    public String [] selectWhere(String what, String table, String where)
    {
    return generalQuery("SELECT " + what + " FROM " + table + " WHERE " + where);
    }
    /**
    * Run a INSERT INTO query in a particular table.
    * Given what query pattern
    * and the table to run query.

    public String [] insert(String table, String colNames, String colValues)
    {
    return generalQuery("INSERT INTO" + table + colNames + " VALUES " + colValues);
    }

    /**
    * Run a general query. The results are printed to standard output.
    * @param query The query.
    */
    ................etc

    StaffidServlet :

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;
    import java.util.Date;
    import java.sql.*;
    import java.sql.Timestamp;
    import java.lang.System.*;

    public class StaffidServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    PrintWriter out;

    response.setContentType("text/html");

    out = response.getWriter();

    out.println("<html><body>");

    /* This string parameters will capture information that is received from webpage and pass it through
    the array */


    String [] staffinfo = request.getParameterValues("staff_id");
    String firstStaffinfo = staffinfo [0];

    Database2 db = new Database2();

    String [] staffresults = db.selectWhere("*", "staffmembers", "staff_id= '"+ firstStaffinfo +"'");


    String [] fileinfo = request.getParameterValues("frefcode");
    String firstFileinfo = fileinfo [0];

    String [] fileresults = db.selectWhere("*", "file", "frefcode= '"+ firstFileinfo +"'");

    long currentTime = System.currentTimeMillis();
    Timestamp ts = new Timestamp (currentTime);

    /* Insert into database*/


    /*String table= "Loan";
    String colNames= "(staff_id, frefcode, dateBorrowed)";
    String colValues= "('" + firstStaffinfo +"'," + firstFileinfo + "," +ts.toString() +")";*/

    String [] table= "Loan";
    String [] colNames= ("staff_id", "frefcode", "dateBorrowed");
    String [] colValues= "('" + firstStaffinfo +"'," + firstFileinfo + "," +ts.toString() +")";

    db.insert(table, colNames, colValues);
    out.println(ts);
    out.println("<br>");...
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    We need the definition of the generalQuery method and the exact error message. I also note you have changed the 'table' variable to a String[]. This is going to cause you problems.

    EDIT: and please use the code /code vB tags when posting code.
    http://forums.devshed.com/misc.php?a...bbcode#buttons

IMN logo majestic logo threadwatch logo seochat tools logo