Solved

Connect to SQL Database

Posted on 2002-03-20
12
181 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countPairs challenge 7 58
json format text only 4 65
What is JNDI datasource in spring 1 37
JDeveloper 12c for 32 bit 4 34
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now