7/11/2012

Java database connectivity with MySQL Server Databse

easywayprogramming.com java database connectivity with mysql database

Before executing following program follow instruction in following link:
database connectivity instruction 

my_sql.java
import java.sql.*;
import java.io.*;

public class my_sql
{
    public my_sql()
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.println("Driver loaded!!!!!!!!!");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/employee","root","ashu");
            System.out.println("connection made!!!!!!!!!");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select *from emp");

            while (rs.next())
            {
                 System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t\t\t"+rs.getString(3)+"\t\t\t"+rs.getString(4));
            } //end while

            con.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

       public static void main(String[] args)
    {
        new my_sql();
    }
}


click here: Back to Tricks/Solution

No comments:

Post a Comment