Results 1 to 5 of 5
  1. #1
    psm
    psm is offline Member
    Join Date
    Apr 2009
    Location
    India
    Posts
    2
    Rep Power
    0

    Default Hello all I joined this forum today

    Hello I joined newly in this forum today. Presently I am fiddling with advanced Java concepts. I am trying to handle databases through Java program. I could connect to mysql database, insert into table and query it. Now I want to create a table in database by passing the name of the table as a parameter. My code to do this is as follows:
    Code:
    public void createTable(String tableName){
    connectToDB();
    String statement = "create table ?(loginname varchar(12),rollnum varchar(15),stuname varchar(20),course char(4),sem char(2))";
    try{
    PreparedStatement st = conn.prepareStatement(statement);
    st.setString(1,tableName);
    st.executeUpdate();

    }catch(SQLException e){
    e.printStackTrace();
    }

    }
    end of code:
    Now when I give table name in the statement it works fine and creates table. But when I give it as a parameter I get the following error:

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''student'(loginname varchar(12),rollnum varchar(15),stuname varchar(20),course c' at line 1

    Can somebody tell me how to do it, if it is possible.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You can only use the '?' at a spot where a "value" is expected, not an "identifier" of any sort, whether that be a column identifier, a schema identifer, a type identifier, or a table identifier. Only "values". The only way you could do this is to use a String concatenation. And then check whether any special characters are contained in the name that would cause you to have to use quotes around the table name. See the SQL reference manual for the DB in question.

  3. #3
    psm
    psm is offline Member
    Join Date
    Apr 2009
    Location
    India
    Posts
    2
    Rep Power
    0

    Default

    Thank you for the reply. Now I have another problem. This time in servlets. I created a java ee application and a ejb that converts temerature from one system to another. Then in the index.jsp, i am taking temperature and type of temperature (degrees C or F) from a form and calling a tempservlet. the servlet user the ejb and calculates the new temp. now I want to deligate display of the result to a jsp called showConversion.jsp. My code for this is as follows:

    request.setAttribute("newtemp",equiv);
    System.out.println("request = " +request);
    RequestDispatcher rd = getServletContext().getRequestDispatcher("/showConversion.jsp");
    System.out.println("rd= " +rd);
    rd.forward(request, response);

    The servlet is in the war file/source packages/servlets and jsps are in warfile/webpages folders respectively.

    Now I am getting a nullpointer exception that points to getRequestDisatcher line. the program prints the required output in console. When I used the same technique in a java web application that ran well without errors. I feel the path of jsp is not correct. but using full path did'nt help.
    the

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Please try by removing the print statments.

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    It will be getServletContext() that is returning a null value, but I'm not familiar with servlets so I can't really help.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Just Joined
    By Shoshana Hartman in forum Introductions
    Replies: 0
    Last Post: 12-23-2008, 03:30 PM
  2. Hi, I am new one for this forum...
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 0
    Last Post: 11-02-2008, 02:20 PM
  3. Inventory Program Part 3 - DUE TODAY (7/28/08)
    By ljk8950 in forum New To Java
    Replies: 7
    Last Post: 07-27-2008, 11:28 PM
  4. Welcome to our new forum: Forum Lobby
    By JavaForums in forum Forum Lobby
    Replies: 18
    Last Post: 02-07-2008, 06:40 AM
  5. hi today i have joined in the forum
    By nagesh_113 in forum Introductions
    Replies: 1
    Last Post: 07-29-2007, 08:03 AM

Posting Permissions

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