Solved

How to access a query using SQLServer 6.5 in a Java Servlet

Posted on 2000-03-13
13
261 Views
Last Modified: 2013-11-24
Can anyone give me a code example using Microsoft's SQLServer 6.5 in a Java Servlet and/or a Java Applet? Especially the Connection string and syntax.
0
Comment
Question by:7of9121098
  • 5
  • 4
  • 3
  • +1
13 Comments
 

Expert Comment

by:g_senthilkumar
ID: 2613160
Hi
  I dont get u what u r asking.But if u want to get the query string methods are available in the HttpServletRequest class.
  So refer JSDK,in HttpServletRequest class u can find out that method.
0
 

Author Comment

by:7of9121098
ID: 2613234
Basically what I want to do is Query a table from Microsoft SQLServer that is in my network. I would like to see an example where a Java Applet and/or Servlet Queries a table from SQLServer.
0
 
LVL 1

Accepted Solution

by:
calvinrsmith earned 50 total points
ID: 2614204
I can query sql server in my servlets my using the jdbc:odbc driver.  

my code:
   // variables used for database object
   Connection conn = null;
   Statement stmt = null;
   ResultSet rset = null;
   
   // query sql server
   try
   {

      // load jdbc:odbc driver
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   
      // Connect to the database
      // change MyDB to the name of your odbc name, change sa to the database password, and change the last paramter to the password of the database
      conn = DriverManager.getConnection ("jdbc:odbc:MyDB","sa","");
     
      stmt = conn.createStatement();
   
      // build the query
      String strQuery = "SELECT * from users";
   
      // run the query
      rset = stmt.executeQuery(strQuery);
   
      while (rset.next())
      {
         System.out.println("id: " + rset.getString("id") + " " + rset.getString("username"));
      }
      System.out.println("that's all folks");
   }
   // handle errors
   catch(Exception e)
   {
      System.out.println(e);
      e.printStackTrace();
   }
   // close the connections
   finally
   {
      if (rset != null)
         rset.close();
      if (stmt != null)
         stmt.close();
      if (conn != null)
         conn.close();
   }

0
 

Author Comment

by:7of9121098
ID: 2614330
Cool example CalvinRSmith, Question:
I believe your example assumes that you are running the JSP Server and SQLServer on the same machine. What if the JSP-Server and SQLServer are in different machines. Like for example in ASP to access a SQLServer table and database you use:

CNString="driver={SQLServer};server=MyServer;database=MyDatabaseName;uid=MyUid;pwd=MyPassword"

How would I do this in JDBC: Reference the Name of the Server by IP address or Name, and specifiy the name of the Database etc.

 Help....



0
 
LVL 2

Expert Comment

by:muraliram
ID: 2614973
Dear 7of9,


    JDBCODBC will not work properly with applets.  If you want to be the database server and web server in different machines you can use IDS server which is available from www.idssoftware.com for evaluation. Modify the code like this.


Class.forName("ids.sql.IDSDriver");
   
      // Connect to the database
      // change MyDB to the name of your odbc name, change sa to the database password, and change the last paramter to the password of the database

 conn = DriverManager.getConnection ("jdbc:ids://databasehost:12/conn?MyDb","sa","");

In this case the web server and database server can be on different machines. IDSDriver is cool. Try
don't forget to add the zip containing
ids.sql package to your classpath which will be there in the IDSinstallation\classes directory.


Try

All the best

       
     
0
 
LVL 1

Expert Comment

by:calvinrsmith
ID: 2616697
7of9,
   to access sqlserver on a difrent machine create an odbc connection the the servlet machine that uses the sqlserver on a diffrent machine.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:7of9121098
ID: 2616723
CalvinRSmith how do I create an odbc connection the the servlet machine that uses the sqlserver on a diffrent machine. Can you please give me a detailed example, I'm new to Java programming. Thanks....
0
 
LVL 2

Expert Comment

by:muraliram
ID: 2618388
Dear 7of9,

 Try what I have told. It will satisfy your need.IDSDriver is very good.

Bye

Murali
0
 
LVL 1

Expert Comment

by:calvinrsmith
ID: 2619637
Murali,
   IDSDriver is a good choice for applet development.  If 7of9 is doing servlet development then hitting jdbc:odbc is the best choice.

7of9,
   Creating an odbc connection is not java but rather windows.  here is how to do it:

1) goto start menu -> settings ->   control panal -> odbc data source
2) click the system tab
3) click the add button
4) click on sql server driver
5) click finish
0
 
LVL 1

Expert Comment

by:calvinrsmith
ID: 2619642
6) enter a name of your choice in the name box
7) enter the name of the sql server in the server box
8) keep hitting next to finish the connection
9) in my code replace MyDB with the name you picked in step 6

Your done!
0
 
LVL 2

Expert Comment

by:muraliram
ID: 2619675
Dear calvinrsmith ,

       Still you require IIS to use
JdbcOdbc.Because IDSDriver is written entirely in java you can use it anywhere right? If you are using IIS JdbcOdbc is OK. But if you want to be the database server and webserver on different machines,then I think IDSDriver id the best choice. right?
If I am going wrong please correct me.OK

Bye

Murali
0
 
LVL 1

Expert Comment

by:calvinrsmith
ID: 2619731
Murali,
  No, JdbcOdbc does NOT use IIS.  It simply uses windows odbc to implent the java jdbc driver.  
   What IDSDriver does is to implement jdbc by tunneling all requests through a web server.  This allows applets to use jdbc since the applet is basically telling webpages to hit the database instead of hitting it directly.  Internally IDSDriver still uses ODBC so you will still have to configure the odbc connection as well as a web server for IDSServer to use.  
   Note the following from the faq of IDSServer:

G2. What database does IDS Server support?
All database systems that support ODBC. For example, MS Access, MS SQL Server, Sybase, Oracle, Informix, DB2, dBase, FoxPro, Paradox, BTrieve, etc. In short, if you can find an ODBC driver, IDS Server will work with the corresponding database.
 
G3. Is IDS Server platform independent?
IDS Server is a client/server product. The client side is 100% platform independent. Web applications based the IDS HTML Extensions or the JDBC API can be deployed to and run by any computers with an internet browser. The server side however is currently limited to Intel based Windows NT and Windows 95.
 
____


   As you can see ODBC is used, and only windows is supported as a server.

Calvin
0
 

Author Comment

by:7of9121098
ID: 2781239
Thanks....
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now