Thread: Java Gui for Postgress

Java Gui for Postgress

From
queries
Date:
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.


Re: Java Gui for Postgress

From
Craig Ringer
Date:
queries wrote:
> 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?

This looks a lot like a uni assignment:

> Class.forName("org.postgresql.Driver"); // Remeber Last Lecture?

For your "freeze" problems: run it under the debugger in netbeans. When
it "freezes", pause execution and look at the event dispatch thread
(EDT); see what it's doing and what it's waiting for.

You really haven't provided enough information, or complete enough code,
to give you more of an answer. What GUI? I don't see anything GUI
related in the code you posted.

I'm not sure this is relevant to you, but as general advice: Make sure
you never do anything affecting the GUI from any thread except the event
dispatch thread. Ever. Use deferred tasks (ref: SwingTask, Runnable,
etc) if possible, or use typical thread synchronization mechanisms.

--
Craig Ringer

Re: Java Gui for Postgress

From
queries
Date:
Hi Craig,

Thank you for your reply and for all of the pointers. I didn't include the
Gui code (It would have taken too much space), just the simple query code
that already works in printing out the results I wanted. Basically I just
wanted to find out how to use this code which works, inside of a Gui by
pressing a button and for results to be displayed inside of a text box.
Basically what code to include under
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {? I've played
around with the code, but haven't been able to make it work yet. I'm very
new to java, we've had only two one hour tutorials so far on this in a class
dedicated to relational databases. It will probably be something simple.

Michael


Craig Ringer wrote:
>
> queries wrote:
>> 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?
>
> This looks a lot like a uni assignment:
>
>> Class.forName("org.postgresql.Driver"); // Remeber Last Lecture?
>
> For your "freeze" problems: run it under the debugger in netbeans. When
> it "freezes", pause execution and look at the event dispatch thread
> (EDT); see what it's doing and what it's waiting for.
>
> You really haven't provided enough information, or complete enough code,
> to give you more of an answer. What GUI? I don't see anything GUI
> related in the code you posted.
>
> I'm not sure this is relevant to you, but as general advice: Make sure
> you never do anything affecting the GUI from any thread except the event
> dispatch thread. Ever. Use deferred tasks (ref: SwingTask, Runnable,
> etc) if possible, or use typical thread synchronization mechanisms.
>
> --
> Craig Ringer
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>
>

--
View this message in context: http://www.nabble.com/Java-Gui-for-Postgress-tp23704425p23716813.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.