Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to Run PostgreSQL Query in JSP?!

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to Run PostgreSQL Query in JSP?!

    Hi every body
    I search in google for JSP file to run PostgreSQL query , I find below code :
    <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.* " %>
    <%@ page import="java.io.*" %>
    <%
    try {
    String driver = "org.postgresql.Driver";
    String url = "jdbc:postgresql://192.168.1.2:4444/dbname";
    String username = "admin";
    String password = "123456";
    String myDataField = null;
    String myQuery = "SELECT datname FROM pg_database";
    Connection myConnection = null;
    PreparedStatement myPreparedStatement = null;
    ResultSet myResultSet = null;
    Class.forName(driver).newInstance();
    myConnection = DriverManager.getConnection(url,username,password);
    myPreparedStatement = myConnection.prepareStatement(myQuery);
    myResultSet = myPreparedStatement.executeQuery();
    if(myResultSet.next())
    myDataField = myResultSet.getString("dispname");
    out.print(myDataField);
    }
    catch(ClassNotFoundException e){e.printStackTrace();}
    catch (SQLException ex)
    {
    out.print("SQLException: "+ex.getMessage());
    out.print("SQLState: " + ex.getSQLState());
    out.print("VendorError: " + ex.getErrorCode());
    }
    %>

    I run this query to get all database names :
    SELECT datname FROM pg_database

    But I got below error :
    SQLException: The column name dispname was not found in this ResultSet.SQLState: 42703VendorError: 0

    What is my problem?
    Please give me a simple JSP file to run PostgreSQL query and see qurey's result

    Regards


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: How to Run PostgreSQL Query in JSP?!

    It says the column 'datname' is not in the table in the FROM clause. It seems to be working properly, maybe you've just made a little mistake with the column name? I see later you retrieve the value of the column 'dispname'. If 'datname' was right, then 'dispname' wouldn't be in the query result. If 'datname' is correct, then you won't be able to retrieve 'dispname'.

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    Akoori (April 15th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to Run PostgreSQL Query in JSP?!

    Thanks
    I want to see My Query result
    SELECT datname FROM pg_database this is true PostgreSQL query
    I want JSP file to show result of it for me
    That mean I want a way to see result of query in above JSP file

    Regards

Similar Threads

  1. Replies: 0
    Last Post: June 20th, 2011, 07:56 AM
  2. Need help to connect to PostgreSql
    By stab in forum JDBC & Databases
    Replies: 3
    Last Post: June 3rd, 2011, 09:01 AM
  3. update query is firing first then insert query
    By salmondavid88 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 8th, 2011, 10:15 AM
  4. Problem with accessing result set with postgresql
    By vrp in forum JDBC & Databases
    Replies: 2
    Last Post: December 23rd, 2010, 07:10 AM
  5. Problem with accessing result set with postgresql
    By vrp in forum Member Introductions
    Replies: 2
    Last Post: December 23rd, 2010, 07:10 AM