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