Sample JDBC Connection Example JAVA,JDBC,MYSQL

/**
 * 
 */
package DatabaseConnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


/**
 * @author satyanarayanagokavarapu
 *
 */
public class MySqlConnectionExample {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  // creates three different Connection objects
  Connection conn2 = null;

  try {

   String url2 = "jdbc:mysql://localhost:3306/soccer?
   user=root&password=mysql123";
   conn2 = DriverManager.getConnection(url2);
   if (conn2 != null) {
    System.out.println("Connected to the database soccer");
   }
  } catch (SQLException ex) {
   System.out.println("An error occurred. Maybe user/password is
 invalid");
   ex.printStackTrace();
  }
 }

}

MYSQL - SELECT Database

How to SELECT a Database in MySql
1. Open your terminal. 

2. Connect to MySql.If not use below link to know how to connect.

How to Connect to MySql Database using MAC terminal

3. Once you logged into your mysql then type the below command 
to SELECT a database (OMS is database name).

mysql> use OMS; Database changed;

Which says Database 'OMS' is selected. 4. To check whether Database OMS selected or not type below command.

mysql> SELECT DATABASE(); +------------+ | DATABASE() | +------------+ | oms | +------------+ 1 row in set (0.00 sec)

Which says Database 'OMS' is selected.
***********Happy Learning*************

MYSQL - Drop Database

How to Drop a Database in MySql
1. Open your terminal. 

2. Connect to MySql.If not use below link to know how to connect.

How to Connect to MySql Database using MAC terminal

3. Once you logged into your mysql then type the below command 
to drop a database (OMS is database name).

mysql> drop DATABASE OMS;

4. To check whether Database OMS dropped or not type below command.

mysql> USE OMS; ERROR 1049 (42000): Unknown database 'oms'

Which says Database 'OMS' no more exits.
***********Happy Learning*************

MYSQL - Create Database

How to Create a Database in MySql
1. Open your terminal. 

2. Connect to MySql.If not use below link to know how to connect.

How to Connect to MySql Database using MAC terminal

3. Once you logged into your mysql then type the below command 
to create a database.

mysql> CREATE DATABASE OMS; Query OK, 1 row affected (0.00 sec)

1 row affected means Database is created successfully. 4. To use this Database OMS type below command.

mysql> USE OMS; Database changed

5. Check which database you are using by using below command.

mysql> SELECT DATABASE(); +------------+ | DATABASE() | +------------+ | oms | +------------+ 1 row in set (0.00 sec)

***********Happy Learning*************

How to Connect to MySql Database using MAC terminal

How to Connect to MySql Database using MAC terminal
1. Open your terminal. 

2. Check your current directory by typing pwd.

$ pwd

Output :

/usr/local/mysql/bin

your directory should be the bin folder of mysql else go to that directory by typing.

$ cd /usr/local/mysql/bin

3. When you are in mysql bin folder login to your mysql database using below command and enter password.

$ ./mysql -u root -p Enter password:

Output :

Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 93 Server version: 5.7.19 MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

If you see above welcome message then you are logged in to your mysql database.

Java - JDBC Connection Example-1.

Java-JDBC Connection Example-1. Problem Description
How to connect to a database using JDBC?

Assume that database name is OMS(OrderManagementSystem) and it has table named orders which has 5 records.

Featured Post

H1B Visa Stamping at US Consulate

  H1B Visa Stamping at US Consulate If you are outside of the US, you need to apply for US Visa at a US Consulate or a US Embassy and get H1...