How Connect MS-SQL in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kanmbk
    New Member
    • Jul 2007
    • 6

    How Connect MS-SQL in java

    Hi

    I write a simple program to connect MS-SQL in java. But it not work fine.

    So i want the exact code to connect MS-SQL with java.


    Thanks
    Bharathi..
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    #2
    Originally posted by kanmbk
    Hi

    I write a simple program to connect MS-SQL in java. But it not work fine.

    So i want the exact code to connect MS-SQL with java.


    Thanks
    Bharathi..
    Hi,
    Plz send the code whatever u hav written

    Thanks and regards,
    madhoriya

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by kanmbk
      Hi

      I write a simple program to connect MS-SQL in java. But it not work fine.

      So i want the exact code to connect MS-SQL with java.


      Thanks
      Bharathi..
      What error messages do you get?

      Comment

      • kanmbk
        New Member
        • Jul 2007
        • 6

        #4
        Hi madhoriya,

        This is the code that i have written,

        [CODE=java] <%@page language="java" %>
        <%@page import="java.io .*"%>
        <%@page import="java.la ng.*"%>
        <%@page import="java.ut il.*"%>
        <%@page import="java.sq l.*"%>
        <%
        String recid="HDR";
        String subid="S002";
        String file_id="X234";
        String trandate="2007/02/14";
        String prod="PROD";
        String filler="Yes";

        try
        {
        Class.forName(" com.microsoft.j dbc.sqlserver.S QLServerDriver" );
        Connection C = DriverManager.g etConnection("j dbc:microsoft:s qlserver://PEOPLET3:1433;D atabaseName=fox ;SelectMethod=C ursor");
        Statement st = C.createStateme nt();
        st.executeUpdat e("INSERT INTO `PDE_HDR_TBL` (`record_id` , `submitter_id` , `file_id` , `transaction_da te` , `prod_cert_test _indicator` , `filler` )VALUES('"+ recid +"' , '"+ subid +"' , '"+ file_id +"' , '"+ trandate +"' , '"+ prod +"' , '"+ filler +"')");
        out.println("in serted");
        }
        catch(Exception e)
        {
        out.println("Ex ception is :"+e);
        }
        %>[/CODE]


        Thanks and regards,
        Bharathi
        Last edited by r035198x; Jul 30 '07, 03:02 PM. Reason: added code tags

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          What's the error/problem you are having?

          You are not passing it login credentials (unless you're passing it some really funky login/pw), is your db set up to allow this?

          Comment

          • madhoriya22
            Contributor
            • Jul 2007
            • 251

            #6
            Originally posted by kanmbk
            Hi madhoriya,

            This is the code that i have written,

            [CODE=java] <%@page language="java" %>
            <%@page import="java.io .*"%>
            <%@page import="java.la ng.*"%>
            <%@page import="java.ut il.*"%>
            <%@page import="java.sq l.*"%>
            <%
            String recid="HDR";
            String subid="S002";
            String file_id="X234";
            String trandate="2007/02/14";
            String prod="PROD";
            String filler="Yes";

            try
            {
            Class.forName(" com.microsoft.j dbc.sqlserver.S QLServerDriver" );
            Connection C = DriverManager.g etConnection("j dbc:microsoft:s qlserver://PEOPLET3:1433;D atabaseName=fox ;SelectMethod=C ursor");
            Statement st = C.createStateme nt();
            st.executeUpdat e("INSERT INTO `PDE_HDR_TBL` (`record_id` , `submitter_id` , `file_id` , `transaction_da te` , `prod_cert_test _indicator` , `filler` )VALUES('"+ recid +"' , '"+ subid +"' , '"+ file_id +"' , '"+ trandate +"' , '"+ prod +"' , '"+ filler +"')");
            out.println("in serted");
            }
            catch(Exception e)
            {
            out.println("Ex ception is :"+e);
            }
            %>[/CODE]


            Thanks and regards,
            Bharathi
            Hi,
            What errors u r getting? I hope there vil be lot of error?

            Thanks and regards,
            madhoriya

            Comment

            • kanmbk
              New Member
              • Jul 2007
              • 6

              #7
              Hi madhoriya,

              Actually i got the following error

              java.sql.SQLExc eption: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.

              Plz help me..


              Thanks..
              Bharathi.

              Comment

              • praveen2gupta
                New Member
                • May 2007
                • 200

                #8
                Hi

                Your are missing driver of sql-server. Go to WEB-INF Then classes folder. now copy and paste sql server driver here.
                Code is correct.

                Comment

                • madhoriya22
                  Contributor
                  • Jul 2007
                  • 251

                  #9
                  Originally posted by kanmbk
                  Hi madhoriya,

                  Actually i got the following error

                  java.sql.SQLExc eption: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.

                  Plz help me..


                  Thanks..
                  Bharathi.
                  Hi,
                  Here is the right syntax for getConnection method :----
                  Code:
                  Connection conn = DriverManager.getConnection("URL","UserName","Password");
                  Make sure u r providing these data correctly. Here URL is the location where ur database server is residing, and rest two variables u can understand.
                  The error which u r getting is due to wrong socket u r providing, so check it again.

                  thanks and regards,
                  madhoriya

                  Comment

                  Working...