Reputation: 471
I'm currently writing a jdbc client-server socket application in java. I've been having a bit of trouble initializing jdbc as i'm not using an ide, just a text editor and jdk. I have put my jdbc driver in my jdk and jre classpaths C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext and \jre\lib\ext . I also renamed the jar to com.mysql.jdbc.driver , but I have still had no luck. Is this a problem with the driver not being found or is it something else?
Here is my error:
C:\Users\imallin\My Documents>java Provider
Waiting for connection
Connection received from 127.0.0.1
server>Hi welcome to the Cyfieithu Eadar-Theangachadh translation server, please
enter a command. Type cmd to see a list of commands
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Provider.insertData(Provider.java:82)
at Provider.run(Provider.java:40)
at Provider.main(Provider.java:118)
Here is my code:
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Provider{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Connection con;
Provider(){}
void run()
{
Console c = System.console();
if (c == null) {
System.err.println("No console.");
System.exit(1);
}
try{
//1. creating a server socket
providerSocket = new ServerSocket(2004, 10);
//2. Wait for connection
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
//3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Hi welcome to the Cyfieithu Eadar-Theangachadh translation server, please enter a command. Type cmd to see a list of commands");
//4. The two parts communicate via the input and output streams
do{
try{
insertData();
message = (String) in.readObject();
System.out.println("client>" + message);
if (message.equals("register"))
sendMessage("first name?");
String firstName = (message);
sendMessage("first name = " + firstName);
insertData();
if (message.equals("listlanguages"))
sendMessage("English \n Thai \n Geordia \n Gaelic \n Welsh \n Gaelic \n Urdu \n Polish \n Punjabi \n Cantonese \n Mandarin");
if (message.equals("bye"))
sendMessage("bye");
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
private void insertData()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost/translator","root","");
String sql = "Insert INTO users (ID, firstName) VALUES ('123','123')";
Statement statement = con.createStatement();
statement.execute(sql);
}
catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Provider server = new Provider();
while(true){
server.run();
}
}
}
Upvotes: 0
Views: 2454
Reputation: 15965
Like other answers have hinted already, the jdbc Jar is not on your classpath.
To fix this, you need to declare, when running your Java main file, that it should use that Jar, by doing:
java -cp "nameOfJar.jar" Provider
This is assuming that your jar is on the same dir as your Provider file, if not you should specify the path to it.
It's totally optional, but I like having a directory named "lib" where I put all the jars I need to run my project, then you can add to classpath by doing:
java -cp "./lib/*;." NameOfJavaClassWithMain
Ultimately, you might end up having several directories or jars that you need to add to your classpath, you do this by using the ;
(:
in linux) separator, like:
java -cp "./lib/*;./conf/configurationFile.properties;." NameOfJavaClassWithMain
If your .class is not on one of the Jars, you might have to add the local directory to your classpath:
java -cp "./lib/*;." NameOfJavaClassWithMain
If you have a package declaration on your class (you should), you need to reference the class by that fully classified name (package+ClassName), like so:
java -cp "./lib/*;." my.package.NameOfJavaClassWithMain
Upvotes: 1
Reputation: 2921
Ultimately your classpath for jdbc is not set.
This is what I do ( a bad practice )
extract your jar file. (say you are working in home directory ~, and the name of directory where jar file is extracted is odbc) then set your classpath to
export CLASSPATH=~/odbc:
( do not forget to add :
after the path ;-) )
Oh could not realize that you are working on windows machine.
Then
set environment variable ( create if does not exists ) CLASSPATH to c:\odbcdir
or other-way is to use java -cp pathToJAR.jar Main
.
Upvotes: 1
Reputation: 12809
The MySQL Driver jar is not on the classpath. You need to fix this, by using the -cp
argument tojava
.
Upvotes: 0