Databases| SQL| MySQL| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
MySQL Connection String 
 

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

 

                         

» View all related tutorials
Related Tags: sql c performance query orm ui process form application io sed find using app for index call record records to

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.