Java Scripts

From Wickle Wiki

Conexion simple contra una base de datos Mysql

Usaremos el driver mysql-connector-java-3.0.11-stable-bin.jar , que debemos bajar de la web del mysql.

Este simple ejemplo nos vale para testear la conexion contra una base de datos Mysql:


import java.sql.*;

public class Connect {
    public static void main(String argv[]) {
        Connection con = null;

        try {
            // here is the JDBC URL for this database
            String url = "jdbc:mysql://192.168.0.69/test?user=root&password=secret";
            // more on what the Statement and ResultSet classes do later
            Statement stmt;
            ResultSet rs;

            // either pass this as a property, i.e.
            // -Djdbc.drivers=org.gjt.mm.mysql.Driver
            // or load it here as we are doing in this example
            Class.forName("org.gjt.mm.mysql.Driver");
            // here is where the connection is made
            con = DriverManager.getConnection(url);
        }
        catch( Exception e ) {
            e.printStackTrace(  );
        }
        finally {
            if( con != null ) {
                try { con.close(  ); }
                catch( Exception e ) { }
            }
        }
    }
}
Personal tools
This site is powered by Queres Tecnologias