Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2000-03-13
13
268 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 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