Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sql Mysql-example MySQL Connection String

Related Tutorials


 
 

Share on Google+Share on Google+

MySQL Connection String

Advertisement
This section gives you brief description of 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

Noor

Pinku

Rakesh

Vikash

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 17, 2009

Related Tutorials

Discuss: MySQL Connection String  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
bala
December 9, 2013
JSP with MYSQL

please share me the details of JSP with MYSQL. reg.html->reg.jsp->login.html->login.jsp->order.html->order.jsp JSP means db connection pages.
DMCA.com