Solved

Connect to SQL Database

Posted on 2002-03-20
12
196 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

734 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