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:
- Loading Driver
- Establishing Connection
- Executing Statements
- Getting Results
- Closing Database Connection
Database Table: employee
CREATE TABLE `employee1` ( |
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{ |
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 |