get mysql Local Connection - Java java.sql

Java examples for java.sql:MySQL

Description

get mysql Local Connection

Demo Code


//package com.java2s;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {
    private static Connection connection = null;

    public static Connection getLocalConnection() {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://127.0.0.1:3306/";
        String dbName = "fms";
        String userName = "root";
        String dbPassword = "root";

        try {//from   w w w .  ja  va 2s .  com
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            System.out
                    .println("DBHelper: Check if your jdbc Driver exists!");
            e.printStackTrace();
            return null;
        }

        try {
            connection = DriverManager.getConnection(url + dbName,
                    userName, dbPassword);
        } catch (SQLException e) {
            System.out.println("DBHelper: mysql Connection Failed.");
            e.printStackTrace();
            return null;
        }

        return connection;
    }
}

Related Tutorials