Wednesday, 11 December 2013

jdbc Example program for ResultSet object using Normal Statement

import java.sql.*;
public class GetData
{
 public static void main(String arg[]) throws SQLException,ClassNotFoundException
 {
  Connection con=prepareConnection();
  Statement st=con.createStatement();
  String q="SELECT empno,ename,sal FROM emp;";
  //execute query
  ResultSet rs=st.executeQuery(q);
  //
  while(rs.next())
  {
   System.out.println(rs.getInt(1)+"\t");
   System.out.println(rs.getString(2));
   System.out.println(rs.getDouble(3));
   }
 // System.out.println(rs.getString(2)+"\t");
 
}
public static Connection prepareConnection() throws SQLException,ClassNotFoundException
{
 String d="com.mysql.jdbc.Driver";
 String url="jdbc:mysql://localhost:3306/test";
 String uname="root";
 String pwd="admin";
 //load
 Class.forName(d);
 //obtain the connection
 return(DriverManager.getConnection(url,uname,pwd));
 }//prepared connection()
}

  

No comments:

Post a Comment