Results 1 to 12 of 12
Thread: entering textfile into database
- 06-11-2010, 09:43 AM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
entering textfile into database
I am facing problem in loading connector.Please help me.
Here is my code....
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 {
String driver = "com.mysql.JDBC.Driver";
String url = "jdbc:mysql://localhost/myfiles";
String username = "root";
String password = "123456";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static void main(String[] args)throws Exception {
String id = "001";
String fileName = "textfile.txt";
FileInputStream fis = null;
PreparedStatement pstmt = null;
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
File file = new File(fileName);
fis = new FileInputStream(file);
pstmt = conn.prepareStatement("insert into DataFiles(id, fileName, fileBody) values (?, ?, ?)");
pstmt.setString(1, id);
pstmt.setString(2, fileName);
pstmt.setAsciiStream(3, 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();
}
}
}
After run it occurs errors and displays...
Error: com.mysql.JDBC.Driver
java.lang.ClassNotFoundException: com.mysql.JDBC.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at InsertTextFileToMySQL.getConnection(InsertTextFile ToMySQL.java:15)
at InsertTextFileToMySQL.main(InsertTextFileToMySQL.j ava:28)
Exception in thread "main" java.lang.NullPointerException
at InsertTextFileToMySQL.main(InsertTextFileToMySQL.j ava:42)
Java Result: 1
- 06-11-2010, 04:34 PM #2
java.lang.ClassNotFoundException: com.mysql.JDBC.DriverYou need the class refered to in the error message on the classpath so the JVM can find it.String driver = "com.mysql.JDBC.Driver";
...
Class.forName(driver);
It's probably in a jar file. Find that jar file and add it to the classpath
- 06-11-2010, 07:03 PM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
I am using Netbean 6.8.Please tell me about the JDBC driver.
//String driver="com.MySQL.JDBC.driver"//
Give an explanation about this.I am trying but it's not working.Where i will find it?
- 06-11-2010, 07:48 PM #4
Where did you get the name of the class referenced above?String driver = "com.mysql.JDBC.Driver";
You can probably use Google to find the parts you need.
Class.forName() loads the class and causes some static code in that class to do what needs to be done for your code to work.
- 06-12-2010, 12:48 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
I have found "com.mysql.jdbc.Driver".
Now,please tell me that where should I store It?
As I am new to java,please explain.
Thanks in advance..
- 06-12-2010, 02:03 AM #6
I assume that the drive is in a jar file. Add that jar file to the classpath for the java command.
- 06-12-2010, 05:42 AM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
Path of my Netbeans 6.8 is....
C:\Program Files\Java\NetBeans 6.8\bin
Path of my code is....
C:\Documents and Settings\UJJAL SUTTRA DHAR\JavaLibrary1\src
Please tell me what is the path for that .jar file?
I am trying,but its not working.Help me..
- 06-12-2010, 05:55 AM #8
An example of putting a jar file on the classpath for the java command:
java -classpath somejarfile.jar ProgramName
I have no idea of how to make netbeans do this.
- 06-14-2010, 07:35 PM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
Previous problem is solved.Thanks to everyone.Now I have facing a new one.
this errors shows...
Error: textfile.txt (The system cannot find the file specified)
java.io.FileNotFoundException: textfile.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
at InsertTextFileToMySQL.main(InsertTextFileToMySQL.j ava:31)
Exception in thread "main" java.lang.NullPointerException
at InsertTextFileToMySQL.main(InsertTextFileToMySQL.j ava:42)
Java Result: 1
My question is where should I place the .txt file.??
- 06-14-2010, 07:46 PM #10
Are you inside an IDE or outside?
If outside, put the file in the same folder with the class files.
If inside ???
- 06-15-2010, 01:53 AM #11
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
I know that.But I am using Netbeans 6.8.Where should i keep this .txt file to read from?
Please help me.
- 06-15-2010, 01:55 AM #12
Similar Threads
-
java error encountered while entering data in the database.security.accessControlexce
By shallz in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-19-2009, 07:38 AM -
Entering a space as argument
By Arne Bjarne in forum EclipseReplies: 1Last Post: 03-02-2009, 12:43 PM -
Entering string in a loop with method
By Konex in forum New To JavaReplies: 3Last Post: 03-01-2009, 07:33 AM -
Entering a while loop with a not equal to string
By bri1547 in forum New To JavaReplies: 9Last Post: 07-09-2008, 07:10 AM -
How to stop entering Data
By adeeb in forum AWT / SwingReplies: 1Last Post: 06-07-2008, 02:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks