Saturday, October 9, 2010

Sore Vulva After Birth

Connecting to PostgreSQL using JDBC

Well, frankly, this is the first time I post code and the idea that nothing is to publish the best source of history, but help provide a guide for programmers who need it in some way. So hopefully my efforts will be helpful.
Normally one of the first things we do when starting a new system is to create modules for connection to the database, so let's start there also.
Today we will see a little class to connect to a Postgres database via JDBC.
depart code simpler and ordering go to get the way we seem the most appropriate.
create a class then we will call simply " Conexion" with the following code:


/ *
* import the necessary libraries to connect
* /
import java.sql.Connection;
java.sql.DriverManager import;


/ * define and publish
*
class * / public class Connection

{
/ * * create a variable
static Connection type
* that will take our connection all the time
* to run the program or until such time that the
* cut it
program * / protected static Connection connection
;

public static void main (String [] args)
{


try {/ *
; * create the url link where
* SERVER: Is the name or server IP address database
* Dbname: The name of the database which we wish to connect
* USER: Our user base
data * Password: The password of our user database
* /
String url = "jdbc: postgresql: / / SERVER: 5432/BASE_DE_DATOS"
; Class.forName ("org.postgresql.Driver");
; connection = DriverManager.getConnection (url, "username", "PASSWORD");
/ *
* If the connection is made with successfully launched a
message * /
System.out.println ("Connection Successful");
/ *
* Once the task
close the connection * /
conexion.close();
        }
        catch (Exception e)
        {
            /*
             * In the event of any error in the code during execution
* skip a message and printed on the console trace of
exception * /
; e.printStackTrace ();
System.out.println ("FAILED TO MAKE THE CONNECTION");

}}}



Ok, I think the code is well explained here, now that we begin to shape connection so that the code can serve.
first thing we do is separate the method of connecting the disconnect method, defining each as a different method and outside the method main ().


import java.sql.Connection;
java.sql.DriverManager import;


public class Connection {protected static Connection connection
;

public static void main ( String [] args)

{
connect ():
, close ();
}

public static boolean login () {

try {

; String url = "jdbc: postgresql: / / SERVER: 5432/BASE_DE_DATOS"
Class.forName ("org.postgresql.Driver ");
connection = DriverManager.getConnection (url," username "," PASSWORD ");

System.out.println ("CONNECTION Successful ");

return true

} catch (Exception e) {

e.printStackTrace ();
System.out.println ("FAILED TO MAKE THE CONNECTION");

return false;}

}


public static boolean close ()
{
try {

conexion.close ();

System.out.println ("The connection was closed successfully");
return true;
}

            catch (Exception e)
            {
                e.printStackTrace();

                System.out.println ("Failed to close connection");
; return false;}


}}


Running The above code simply connect our application to the database, we launched a message that the connection has been established, and finally we disconnect again send a message that the connection has been terminated. Already
end add two more methods that are to execute our query in sql. For those who are wondering why I answer two methods simple. Sql commands to send to our database we use a class called Statement which, among all methods have two that we will take over the hand and methods executeQuery () executeUpdate and () .
Said in the quickest and easiest way possible executeQuery () used to send any questions and bring results, and the executeUpdate () is to send the data update command such as INSERT, UPDATE and DELETE.
also use the ResultSet object to make the results of its consultations to the database. So add two more methods with which we will insert, update and delete data and query data. Our code would look like.


import java.sql.Connection;
java.sql.DriverManager import;

import java.sql.Statement;
import java.sql.ResultSet;

public class Conexion
{
    protected static Connection conexion;

    private static Statement stmt;

    public static void main(String[] args)

    {
        /*
    * application connects to the database
* /
: connect ():
/ *
* Insert a new record to a table called "cargo" that has two fields: id, description
* /

if (execute ("INSERT INTO post (id, description) VALUES (1, 'Collector')"))
{
/ *
insertion * If registration is successful the method We will return "true"
* now send the query to query using the "see" and save the
* Result in a variable of type ResultSet

* /

ResultSet result = query ("SELECT id, description FROM post WHERE id = 1"); ;
/ *
* If the query brought results move the cursor to the first record, this method returns TRUE
* when the list of results containing at least one record
             */

            if(resultado.first())
            {
  / *
; * Once placed the cursor in the first register your details printed on the console
; * The ResultSet can return the data type we ask where possible
, * to get methods. For the "id" we could have a return getString and we
* The data as a string, as the data is numeric we could make a return getInt and we
* the data as an Integer (Provided it is within the range of numbers allowed for an Integer)
; * /
System.out.println (" CONSULTATION returned: "+
resultado.getString("id")+" "+
                                                 resultado.getString("descripcion"))

            }
        }
        /*
         * Finally close the connection
* /
close () ;

}

public static boolean connect ()
{
        try
       {
            String url = "jdbc:postgresql://SERVIDOR:5432/BASE_DE_DATOS";
            Class.forName("org.postgresql.Driver");
            connection = DriverManager.getConnection (url, "username", "PASSWORD");
System.out.println ("Connection made with success ");

return true

;}
catch (Exception e) {

e.printStackTrace ();
System.out.println ("Could not connect");

return false;}
}


; public static boolean close ()

{if (connection! = null)

{
            try

            {
                conexion.close();

                System.out.println ("Connection successfully closed");
return true;}

catch (Exception e)
            {
                e.printStackTrace();

                System.out.println ("Failed to close connection");
; return false;

}}

else

{System.out.println ( "The connection is already closed");
return true;
;}
}


public static boolean execute (String sql)
{
; try

{
; / *
* Create the Statement in connection
, * / stmt = conexion.createStatement
();

/ *
, * execute the SQL statement using the method executeUpdate ()
* If no errors back guarantee TRUE
             */             stmt.executeUpdate(sql);
            return true;
        }
catch (Exception e)
{
e.printStackTrace ();
System.out . println ("Error: Could not perform operation");
            return false;
        }
    }

    public static ResultSet consultar(String sql) 

    {
        /*
* start the variable result will contain the query result
, * /
ResultSet result = null;
try

; {
/ *
; * Create the Statement prepare for a visit by two parameters
, * ResultSet.TYP E_SCROLL_SENSITIVE: Indicates that you can travel freely between
*     registros devueltos
             * ResultSet.CONCUR_READ_ONLY: Indica que los resultados solo se usarán para lectura
             *                       as there are ways to update records
, * database through the ResultSet

* /
; Statement stmt = conexion.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 

                                                                            ResultSet.CONCUR_READ_ONLY)
/ *

* We save the results in our ResultSet and return it
; * /
result = stmt.executeQuery (sql);
;}

catch (Exception e)
; {
e.printStackTrace ();
System.out.println ("Error: Could not perform operation");

} return result;}

}


Finally we have a class (although it is just basic) with enough features to connect to a database, to query and manage data using SQL commands.
Hope you enjoyed it and hopefully find critical comments and suggestions for further improvement, any queries can leave it in the comments and posts next try to resolve all doubts.

Wednesday, October 6, 2010

Master Pyragon Snooker Cue Price



Good People!

My name is David Reese, I'm System Analyst and most of all I work in software development, and although I am trying to learn python now mostly java development. My intention
here is to go throwing codes to help those seeking help in developing java applications and the ideas that are coming in programming, design, etc.
I hope everyone benefits from them.


Blessings and much success for everyone!

Monday, October 4, 2010

Shannon Whirry Sliver

"The sun does not rise to tod @ s"

American Harvest Convetion Perfection Co 200t

photos Concentration Hervás