Showing posts with label jsp to sql server connection. Show all posts
Showing posts with label jsp to sql server connection. Show all posts

Tuesday, June 15, 2010

JSP Connectivity with SQL Server

Note :-
For the connectivity with JSP to SQL Server first Download Microsoft SQL Server JDBC 2.0 Driver for java on the bellow 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

After download it gives sqljdbc_2.0.1803.100_enu.exe. Run it, then it will gives a zip file, extract  Zip file. Copy and paste sqljdbc4.jar in  Paste java->jdk->jre->->lib->ext Folder after that restart the Tomcat-Apache webserver.



<%@ page language="java" import="java.sql.*" %>
<%
    Connection con = null;
    Statement stmt =null;
    ResultSet rs = null;
    try{ 
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String url = "jdbc:sqlserver://localhost:1433;username=mysqlusername;password=mysqlpassword;databasename=dbname";
        conn = DriverManager.getConnection(url);
        stmt = con.createStatement();
        out.println("Database connected");
        String queryCountry = "Select * from table";
        ResultSet countResultSet = stmt.executeQuery(queryCountry);
            while(countResultSet.next()){
                    out.println(countResultSet.getString("name"));
                    out.println("\n");
            }
   
      }catch(Exception e){
            out.println(e);
      }

 %>