我目前正在用 java 编写一个 jdbc 客户端-服务器套接字应用程序。我在初始化 jdbc 时遇到了一些麻烦,因为我没有使用 ide,只是一个文本编辑器和 jdk。我已将 jdbc 驱动程序放在我的 jdk 和 jre 类路径 C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext 和 \jre\lib\ext 中。我还将 jar 重命名为 com.mysql.jdbc.driver ,但我仍然没有运气。这是找不到驱动程序的问题还是其他问题?
这是我的错误:
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)
这是我的代码:
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();
}
}
}