RSS

Code java : MySQL connection

14 Jan

Code java example for connect MySQL database:

This is the collection of me: Author : lexuanvu

import java.sql.*;

public class MySQLConnection {

 

    String host = "";

    String username = "";

    String password = "";

    String database = "";

    Connection connect = null;

    Statement statement = null;

    ResultSet result = null;

 

    public MySQLConnection(String host, String username, String password, String database) {

        this.host = host;

        this.username = username;

        this.password = password;

        this.database = database;

    }

 

    protected void driverTest() throws Exception {

        try {

            Class.forName("org.gjt.mm.mysql.Driver");

        } catch (java.lang.ClassNotFoundException e) {

            throw new Exception("MySQL JDBC Driver not found");

        }

    }

 

    protected Connection getConnect() throws Exception {

        if (this.connect == null) {

            driverTest();

 

            String url = "jdbc:mysql://" + this.host + ":3306/" + this.database;

            try {

                this.connect = DriverManager.getConnection(url, this.username, this.password);

            } catch (java.sql.SQLException e) {

                throw new Exception("Cannot connect to Database");

            }

        }

        return this.connect;

    }

 

    protected Statement getStatement() throws Exception {

        if (this.statement == null ? true : this.statement.isClosed()) {

            this.statement = this.getConnect().createStatement();

        }

        return this.statement;

    }

 

    public ResultSet executeQuery(String Query) throws Exception {

        try {

            this.result = getStatement().executeQuery(Query);

        } catch (Exception e) {

            throw new Exception("Error : " + e.getMessage() + " - " + Query);

        }

        return this.result;

    }

 

    public int executeUpdate(String Query) throws Exception {

        int res = Integer.MIN_VALUE;

        try {

            res = getStatement().executeUpdate(Query);

        } catch (Exception e) {

            throw new Exception("Error : " + e.getMessage() + " - " + Query);

        } finally {

            this.Close();

        }

        return res;

    }

 

    public void Close() throws SQLException {

        if (this.result != null && !this.result.isClosed()) {

            this.result.close();

            this.result = null;

        }

        if (this.statement != null && !this.statement.isClosed()) {

            this.statement.isClosed();

            this.statement = null;

        }

        if (this.connect != null && !this.connect.isClosed()) {

            this.connect.isClosed();

            this.connect = null;

        }

    }

}

written by hotdream1990

 
Leave a comment

Posted by on January 14, 2011 in Java

 

Tags: , ,

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s