• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

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

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
7of9121098
Asked:
7of9121098
  • 5
  • 4
  • 3
  • +1
1 Solution
 
g_senthilkumarCommented:
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
 
7of9121098Author Commented:
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
 
calvinrsmithCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
7of9121098Author Commented:
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
 
muraliramCommented:
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
 
calvinrsmithCommented:
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
 
7of9121098Author Commented:
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
 
muraliramCommented:
Dear 7of9,

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

Bye

Murali
0
 
calvinrsmithCommented:
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
 
calvinrsmithCommented:
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
 
muraliramCommented:
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
 
calvinrsmithCommented:
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
 
7of9121098Author Commented:
Thanks....
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now