Java Gui for Postgress

From: queries <michaelkni(at)hotmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Java Gui for Postgress
Date: 2009-05-25 09:58:14
Message-ID: 23704425.post@talk.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


Hi Everyone,

I need to build a simple gui front end with jdbc access to my Postgres
database. I have the front end ready and I have some code which retrieves
records from my database, but for some reason I keep on getting errors
(lately just freezes without errors) when I apply that same code to the
buttons on my gui. It's definitely me doing something stupid because I have
0% knowledge in java. Can anyone help?

Here is the code that works for me

This is my main calling Queries:

Queries query = new Queries ();
query.listDemographics();

This is the code for my queries which retrieve patient names and demographic
address and print it on the screen. Could you please let me know how I can
code the same for the button of my gui in the expression private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

Oh, I'm coding in Netbeans 6.5.1

Queries:

package tutorial3;
import java.sql.DriverManager; // This Loads The Java JDBC DRIVER Manager
import java.sql.Connection; // This Loads the JAVA CONNECTION Manager.
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; // This Loads Exception Handeling Mechanisem
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* 2009.
* @author Me
*/
public class Queries {
/**
* @param args the command line arguments
*/
public static void listDemographics() {
System.out.println("Checking Driver… ");
try { // Try Means Just Try! if Code get faild , it does not exit.
Class.forName("org.postgresql.Driver"); // Remeber Last Lecture?
} catch (ClassNotFoundException cnfe) {
// If we get to this point , this means, some bad things happens.
System.out.println("Error : Something Wrong With the driver. " +
"Have You installed it in your computer?");
cnfe.printStackTrace();
System.exit(1);
}
// Ok it seems we sucessfully passed the danger area!
System.out.println("Driver Loaded and ready to use!");
Connection c = null;
try {
// The second and third arguments are the username and password,
// respectively. They should be whatever is necessary to connect
// to the database.
// Ok, We Loaded the Driver, But we need to use it. this way :
c = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/General_practice", "postgres",
"password");
} catch (SQLException se) {
// If we get to this point , this means, some bad things happens.
System.out.println("Couldn't connect. Something Wrong with Setting? ");
se.printStackTrace();
System.exit(1);
}
// Ok it seems we sucessfully passed the danger area!
if (c != null) {
System.out.println("YES! We connected to the database!");
}

// Now what? ... We Descuss it Next Time, but you can continue by looking at
// sample codes in last week Lecture.
// You may start passing SQL Statements to the Server and Print the Results.
//int license = 1001;
//int year = 2009 ;
PreparedStatement stmt;
try {
stmt = c.prepareStatement("select * " + " from patient");
//stmt.setInt(1, license);
//stmt.setInt(2, year);
/* execute the query and loop through the resultset */
ResultSet rset = stmt.executeQuery();
int nr = 0;
while (rset.next()) {
nr++;
//System.out.println("patient" + rset.getString("pid_internal") +
rset.getString("main_name"));
System.out.println("patient" + rset.getString("main_name")+
("demographic_details" + rset.getString("address")));
//+ rset.getDate("acc_date") + " with car ’" + rset.getString("regno") +
//"’ and damage " + rset.getDouble("damage_amount"));
}
if (nr == 0) {
System.out.println("No entries found.");
}
/* clean up */
stmt.close();
c.close();
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
--
View this message in context: http://www.nabble.com/Java-Gui-for-Postgress-tp23704425p23704425.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2009-05-25 10:18:47 Re: Query
Previous Message Laurent Wandrebeck 2009-05-25 09:32:35 Re: very large tables