Δημοσιεύτηκε: 29 Αύγ 2010, 14:10
από kelas
παιδιά,θέλω την βοήθειά σας...
Προσπαθώ απο ενα αρχείο MyFile.txt να προσθέσω λέξεις σε έναν πίνακα μιας SQL database κ δεν τα καταφέρνω..

MySQL Table Syntax
Κώδικας: Επιλογή όλων

CREATE TABLE `DbJava` (
`name` text NOT NULL,
`surname` text NOT NULL,
`telefon` text NOT NULL,
`country` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1


java code:
Spoiler: show
Κώδικας: Επιλογή όλων

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class InsertTextFileToMySQL {

public static Connection getConnection() throws Exception {

Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/DbJava","root","182100");
return conn;
}

@SuppressWarnings("null")
public static void main(String[] args)throws Exception {
String name = "name" ;
String firstname = "firstname";
String telefon = "telefon" ;
String country = "country";

DataInputStream dis = null;
FileInputStream fis = null;
PreparedStatement pstmt = null;
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
File file = new File("/home/kelas/Desktop/MyFile.txt");
fis = new FileInputStream(file);
while (dis.available() != 0) {
pstmt = conn.prepareStatement("insert into Kelas(name, firstname, telefon,country) values (?, ?, ?, ?)");
pstmt.setString(1, name);
pstmt.setString(2, firstname);
pstmt.setString(3, telefon);
pstmt.setString(4, country);
pstmt.setAsciiStream(4, fis, (int) file.length());
pstmt.executeUpdate();
conn.commit();
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
} finally {
pstmt.close();
fis.close();
conn.close();
}
}
}

και το error:
Spoiler: show
Κώδικας: Επιλογή όλων

Error: com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at InsertTextFileToMySQL.getConnection(InsertTextFileToMySQL.java:12)
at InsertTextFileToMySQL.main(InsertTextFileToMySQL.java:29)
Exception in thread "main" java.lang.NullPointerException
at InsertTextFileToMySQL.main(InsertTextFileToMySQL.java:47)

Επειδή την πέμπτη δίνω δικτυακό προγραμματισμό αν κάποιος δεν βαριέται please να με βοηθήσει...