Solved

Connect to SQL Database

Posted on 2002-03-20
12
191 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java exception bubble up 2 21
hibernate example issues from command prompt 10 50
throw exception 21 58
ejb entity bean example issue 2 18
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

820 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