Skip to content

Connecting MySQL Database from Java with JDBC


First of all download the MySQL connector from

import java.sql.*;

public class ConnectDB {

	public static void main(String[] args) {
		Connection con = null;

		String username ="root";
		String password="password";
		String dbName = "myDatabaseName";

		String url = "jdbc:mysql://localhost:3306/" + dbName;
		String driver = "com.mysql.jdbc.Driver";

		String genre="classic";
		String mood ="happy";

		try {
			con = DriverManager.getConnection(url, username, password);
			try {
				Statement st = con.createStatement();
				int val = st
						.executeUpdate("INSERT INTO test (genre, mood) VALUES('classic','happy')");

	} catch (SQLException s) {
				System.out.println("SQL statement is not executed!");
		} catch (Exception e) {

From → Java Examples

Leave a Comment

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: