Codxplore.com

Latest news:

Sahih al-Bukhari (সহীহ বুখারী) is a free Hadith application for android. This application is advertisement free. Download now https://play.google.com/store/apps/details?id=com.akramhossin.bukharisharif

MySQL database connection using java


MySQL database connection using java

For connecting to MySQL database using java we need to do following.

Java driver class name for MySQL: com.mysql.jdbc.Driver

Database connection string or connection url: jdbc:mysql://localhost:3306/addressBook

  • jdbc is the api name.
  • mysql is the database managment software.
  • localhost is the name of server where MySQL is installed.
  • 3306 is the port port number.
  • addressBook is the name of database.

MySQL username: dbUsername

MySQL password: dbPassword

If we need to connect java application to MySQL  we need to add one jar file to the application. right click on the libraries  under your project propertise , now click on the Add jar/folder and select mysql-connector-java-5.1.23-bin.jar

Below is the example database connection class in java

public class DB {

    Connection con = null;
    Statement st = null;
    ResultSet rs = null;
    ResultSetMetaData md = null;
    String dburl = "jdbc:mysql://localhost:3306/addressBook";
    String dbuser = "root";
    String dbpassword = "";

    public DB() {
        try {
            con = DriverManager.getConnection(dburl, dbuser, dbpassword);
            st = con.createStatement();
        } catch (Exception ex) {
            ex.getMessage();
        }
    }
}

Tags : Java, MySQL,

Views : 1076

Subscribe Us


Follow Us