現在、Java で jdbc クライアント サーバー ソケット アプリケーションを作成しています。私はIDEを使用しておらず、テキストエディタとjdkだけを使用しているため、jdbcの初期化に少し問題がありました。jdk および jre クラスパス C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext および \jre\lib\ext に jdbc ドライバーを配置しました。また、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();
}
}
}