Wednesday, February 3, 2010

JSP connectivity with MS-SQL SERVER

Download Microsoft SQL Server JDBC Driver 2.0 for java on following url:
http://www.microsoft.com/downloads/details.aspx?FamilyID=99b21b65-e98f-4a61-b811-19912601fdc9&displaylang=en

or
http://go.microsoft.com/fwlink/?LinkId=144633&clcid=0x409

It will gives sqljdbc_2.0.1803.100_enu.exe. Run it, will gives a zip, extract them. Copy  sqljdbc4.jar in Apache Tomcat 6.0.14\lib and don't forget to restart the Tomcat-Apache webserver.
Write down the following code, as i think it is understandable.




<%@ page language="java" import="java.sql.*" %>
<%
    Connection conn = null;
    Statement st =null;
    ResultSet rst = null;
    try{  
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String strUrl = "jdbc:sqlserver://localhost:1433;username=jivasingh;password=jivasingh12345;databasename=imt";
        conn = DriverManager.getConnection(strUrl);    
        st = conn.createStatement();
        out.println("connected");
        String queryCountry = "Select * from users";
        ResultSet countResultSet = st.executeQuery(queryCountry);
            while(countResultSet.next()){
                    out.println(countResultSet.getString("name"));
                    out.println("
");
            }
    
      }catch(Exception e){
            out.println(e);
      }
 
 %>
Hahaha....happy .....Gracias....you have just connect with SQL server...

No comments:

Post a Comment