Solved

Connect to SQL Database

Posted on 2002-03-20
12
198 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
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 …

691 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