Monday, March 24, 2014

DB Connection and retrieving multiple data from the table

package DataBasetesting;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ConnectMSSQLServer {

    public static void main(String[] args) {
   
        ConnectMSSQLServer connServer = new ConnectMSSQLServer();

        //connServer.dbConnect("jdbc:sqlserver://localhost:1433;databaseName=Test;", "sa", "NSin@098");
        connServer.dbConnect("jdbc:sqlserver://localhost:1433;databaseName=test;", "sa", "password");
    }
       
   public void dbConnect(String db_connect_string, String db_userid,String db_password)

    {
Boolean isresultSet=false;
    try {

    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    Connection conn = DriverManager.getConnection(db_connect_string,

    db_userid, db_password);

    System.out.println("connected");

    Statement statement = conn.createStatement();

    //String queryString = "select * from sysobjects where type='u'";
   
    String queryString = "select * from Student";
    boolean results = statement.execute(queryString);
//    ResultSet result = statement.executeQuery(queryString);
    int rsCount = 0;
   
     do {
            if(results) {
               ResultSet rs = statement.getResultSet();
               rsCount++;

               //Show data from the result set.
               System.out.println("RESULT SET #" + rsCount);
               while (rs.next()) {
                  System.out.println(rs.getString("stno") + ", " + rs.getString("stname"));
               }
               rs.close();
            }
            System.out.println();
            results = statement.getMoreResults();
            } while(results);
     statement.close();
          }
       catch (Exception e) {
          e.printStackTrace();
       }
   

    }
}

No comments:

Post a Comment

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...