MySQL Connection String

This section gives you brief description of MySQL connection string.

MySQL Connection String

MySQL Connection String

     

This section gives you brief description of MySQL connection string. For the connection in mysql, we need some information about database like server network address, user id, password and name of database. 

 

 

 

 

 

For example,

Server=192.168.10.146;
Port=3306;
Uid=root;
Pwd=root;
Database= sqlexamples;   

Server: The network address to connect with MySQL. The default is localhost (127.0.0.1).
Port
: MySQL port number for connections. Default is 3306.
Uid
: MySQL user account id to connect with MySQL.
Pwd
: MySQL user password to connect with MySQL.
Database
: Name of the database to work upon. 

The above information is used to make a connection string while connecting with the database. In the application below, we have connected to the MySQL database and retrieved the employee names from the database using JDBC. Here are some steps to follow while writing JDBC program: 

  1. Loading Driver
  2. Establishing Connection
  3. Executing Statements
  4. Getting Results
  5. Closing Database Connection

Database Table: employee

 CREATE TABLE `employee1` ( 
`emp_id` int(11) NOT NULL auto_increment, 
`emp_name` varchar(10) character set utf8 NOT NULL, 
`emp_salary` int(11) NOT NULL, 
`emp_startDate` datetime NOT NULL, 
`dep_name` varchar(50) NOT NULL, 
PRIMARY KEY (`emp_id`) 
)

 

Here is the code of java program that retrieves all the employee data from database and displays on the console:

import java.sql.*;  

public class RetriveAllEmployees{ 
 
public static void main(String[] args) { 
 
System.out.println("Getting All Rows from employee table!"); 
 
Connection con = null; 
 
String url = "jdbc:mysql://192.168.10.146:3306/"; 
 
String db = "sqlexamples"; 
 
String driver = "com.mysql.jdbc.Driver"; 
  
 String user = "root"; 
 
String pass = "root"; 
 
try{ 
 
Class.forName(driver); 
 
con = DriverManager.getConnection(url+db, user, pass); 
 
Statement st = con.createStatement(); 
 
ResultSet res = st.executeQuery("SELECT * FROM employee"); 
 
System.out.println("Employee Name: " ); 
 
while (res.next()) { 
 
String employeeName = res.getString("emp_name"); 
 
System.out.println(employeeName );
 
} 
 
con.close(); 
 
} 
 
catch (ClassNotFoundException e){ 
 
System.err.println("Could not load JDBC driver"); 
 
System.out.println("Exception: " + e); 
 
e.printStackTrace(); 
 
} 
 
catch(SQLException ex){ 
 
System.err.println("SQLException information"); 
 
while(ex!=null) { 
 
System.err.println ("Error msg: " + ex.getMessage()); 
 
System.err.println ("SQLSTATE: " + ex.getSQLState()); 
 
System.err.println ("Error code: " + ex.getErrorCode()); 
 
ex.printStackTrace(); 
 
ex = ex.getNextException(); // For drivers that support chained exceptions
 
} 
 
} 
 
}  

}

 

Output:

C:\vinod>javac RetriveAllEmployees.java

 

C:\vinod>java RetriveAllEmployees

Getting All Rows from employee table!

Employee Name:

Vinod

Amar

Ravi

Suman 0

Noor

Pinku

Rakesh 1

Vikash