4.4BSD & CONNECTOR/J – PART 1

Well, here I am. I will introduce Connector/J within examples and one comparison, let’s do the homework:

.1 Getting Connector/J: Info about the fact and formal info -> SOURCE

.2 Installing: Now that you have the file (binary or source) we should unzip or untar it. In this case we us the binary one. After done the first step, we look up for the *.jar, when we got it, we must ubicate it in a safe place, the add it to the classpath:

export set CLASSPATH=/Users/applebook/Desktop/mysql-connector-java-5.1.11/mysql-connector-java-5.1.11-bin.jar:$CLASSPATH

or

setenv CLASSPATH=/Users/applebook/Desktop/mysql-connector-java-5.1.11/mysql-connector-java-5.1.11-bin.jar:$CLASSPATH

Using any of those depends on what we are using (bash, sh, cshell, etc). Hey!, if you are using a webserver like tomcat this tuto is not for you.

Next, open your favorite IDE or geek editor (doesn’t matter which one you like), I use CODA, but you can use VIM or other that makes you happy. The code is simple and fast…but not furious:

import java.sql.*;

public class MysqlConnect{
public static void main(String[] args){
System.out.println(“MYSQL Connect Example.”);
Connection conn = null;
String url = “jdbc:mysql://localhost:3306/”;
String dbName = “DDBB_NAME”;
String driver = “com.mysql.jdbc.Driver”;
String userName = “userName”;
String password = “userPassword”;

try{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println(“Conectado a la base de datos”);
conn.close();
System.out.println(“Desconectado de la DB”);
}catch(Exception e){
e.printStackTrace();
}
}
}

Compile:

$javac MysqlConnect

Execute:

$java MysqlConnect

The result:

sh-3.2# java MysqlConnect
MYSQL Connect Example.
Conectado a la base de datos
Desconectado de la DB

Ok, ok, it’s not the great thing, doesn’t it?. Let’s connect to MySQL from Java, without downloading anything nor configuring the not beauty classpath:

.1 Download NetBeans IDE

.2 Install it

.3 Configure a new project related to J2SE

.4 Name your project as you want

.5 Add in Library MySQL JDBC, copy de code above, click de PLAY BUTTON.

The result is the same.

Advertisement

Deja un comentario

Fill in your details below or click an icon to log in:

Logo de WordPress.com

You are commenting using your WordPress.com account. Log Out / Cambiar )

Twitter picture

You are commenting using your Twitter account. Log Out / Cambiar )

Facebook photo

You are commenting using your Facebook account. Log Out / Cambiar )

Connecting to %s

Seguir

Get every new post delivered to your Inbox.