[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Connect to SQL Database

Posted on 2002-03-20
12
Medium Priority
?
217 Views
Last Modified: 2010-03-31
I would like to connect to an SQL database from within an Applet.  The intention is that a tickertape be fed with data from the db (which resides on the same server from where the applet is loaded).  Can someone please give me details of the connection code?
Thanks in anticipation.
0
Comment
Question by:Chris_m
  • 7
  • 5
12 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6883818
which database?

for mysql it would look something like this:

// Load driver class
Class.forName ("org.gjt.mm.mysql.Driver").newInstance();

// Get the connection
Connection con = DriverManager.getConnection("jdbc:mysql://<server>:3306/<database>?user=<user name>&password=<password>");

// once you have the connection to run a query you can do something like:
Statement stmt = con.createStatment();
ResultSet rs = stmt.executeQuery("Select * from <table>");

0
 

Author Comment

by:Chris_m
ID: 6885060
I am using Microsoft SQL 7.0.  Does the Class.forName that you have given work for that?  I have tried it but it doesn't seem to work (although the compiler doesn't give an error message). The problem with applets is that I cannot display a message indicating success or failure.  
Incidently, the database is called RefMats and user is sa with no password.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6885744
no it won't work... which jdbc driver are you using?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 18

Expert Comment

by:bobbit31
ID: 6885754
if you do not have one yet, see:

http://industry.java.sun.com/products/jdbc/drivers
0
 

Author Comment

by:Chris_m
ID: 6888151
Hello Bobbit31
I have just downloaded "Connect JDBC Type 4 JDBC drivers for high-performance Java database connectivity to all of today's major databases."  But have not installed it yet.  Is this OK.  You will have noticed that I do not know too much about database connectivity, so I hope you will help me.  Because this is turning out to be more involved than I first thought, I will increase the points.
Thanks in anticipation.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6888900
sure it will work...

you would have something like:
Class.forName("com.ddtek.jdbc.sqlserver.SQLServerDriver").newInstance();

and

Connection con = DriverManager.getConnection
 ("jdbc:datadirect:sqlserver://server1:1433;User=test;Password=secret");

if you do the install correctly (ie. setting classpath etc.) then using the above lines instead of the ones i use for my driver and db should work.

good luck.
0
 

Author Comment

by:Chris_m
ID: 6893656
I propose increasing points to 200 if that is agreeable.
In the meantime, I have entered the following code but I do not appear to be getting anything into the array. Also, can you tell me which path I put int CLASSPATH? (ie where the source code is or where the applet resides?

msgArray = new String[11];
         
arraySize = msgArray.length;          
         
try {              
               
Class.forName("com.ddtek.jdbc.sqlserver.SQLServerDriver").newInstance();
          }
          catch(Exception E){}
          try {    
               Connection con = DriverManager.getConnection("jdbc:datadirect:sqlserver://localhost:1433;User=sa");
     stmt = con.createStatement();
     rs = stmt.executeQuery("Select * from EUPRTable");    
     for(i=0; i<11; ++i) {                    
          msgArray[i]=rs.getString("collection");
     }              
     rs.close();
        stmt.close();
        con.close();
}
     catch(SQLException E){}
         
0
 

Author Comment

by:Chris_m
ID: 6898821
Hello bobbit31,
I think I am not getting past the Class.forName because I put a soryt of default value for the Array in the catch(Exception E) and that is run by the applet.  Another thing, the Applet source and the DB are on the same server.
Hope to hear from you.
Regards
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6899335
is the exception being thrown?
show me the stacktrace if so, ie:

Class.forName("com.ddtek.jdbc.sqlserver.SQLServerDriver").newInstance();
         }
         catch(Exception E){ E.printStackTrace(System.out); }

if you run your applet w/ appletviewer you'll get the stacktrace in shell.

also, you should have a jar file containing the package com.ddtek.jdbc.sqlserver.SQLServerDriver.  You want to put this jar file in your classpath.        
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6899345
also, i think you want to do something like this:

Vector msgVector = new Vector();

while (rs.next()) {  
   msgVector.add(rs.getString("collection"));
}

that way you don't have to hard code the 11 in there... it won't matter how many records you have. (if you don't like vector, arraylist will work just as well.)
0
 

Author Comment

by:Chris_m
ID: 6907693
The reason I am hard coding 11 (it should be 10) is that I only want to view the 10 latest records.

I have run the applet in appletviewer and there is a whole bunch of info there; but I think the message you want isjava.io.FileNotFoundException: C:\jdk1.2.2\bin\com\ddtek\jdbc\sqlserver\SQLServerDriver.class (The system cannot find the path specified)

Please tell me how to get the jar file into the classpath because somehow, I don't think I am doing it correctly.
Thanks and regards
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 800 total points
ID: 6909306
i'm assuming that you're os is win9x or nt/2000
in that case, add the path to your jar in your classpath:

ie:

SET CLASSPATH=<path to the jar file containing SQLServerDriver>
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

873 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