Conveting a Text File to a Table
hi,
I am trying to extract a bunch of data from a text file and insert it into a table. To do this i am using MySQL and java (netbeans). I have the code and it works but only for some entries from the text file. The entries it does not work for are the ones that have a space in the title, since i am using the space character as the delimiter. Another issue i have noticed is the fact that some of the titles have another single quote in them therefore it throws an error because it ruins the syntax of the SQL query. Below is all my code, hopefully somone can help me out with this:
These are some of the entries from the text file:
1000000103 50 4.5 "#1 Single" (2006)
2...1.2.12 8 2.7 "$1,000,000 Chance of a Lifetime" (1986)
11..2.2..2 8 5.0 "$100 Taxi Ride" (2001)
....13.311 9 7.1 "$100,000 Name That Tune" (1984)
3..21...22 10 4.6 "$2 Bill" (2002)
30010....3 18 2.7 "$25 Million Dollar Hoax" (2004)
2000010002 111 5.6 "$40 a Day" (2002)
2000000..4 26 1.6 "$5 Cover" (2009)
.0..2.0122 15 7.8 "$9.99" (2003)
..2...1113 8 7.5 "$weepstake$" (1979)
0000000125 3238 8.7 "'Allo 'Allo!" (1982)
1....22.12 8 6.5 "'Allo 'Allo!" (1982) {A Barrel Full of Airmen (#7.7)}
.....422.2 5 7.2 "'Allo 'Allo!" (1982) {A Bun in the Oven (#8.0)}
......4112 7 7.9 "'Allo 'Allo!" (1982) {A Fishy Sendoff (#9.5)}
Code:
import java.sql.*;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]){
try {
FileInputStream fstream = new FileInputStream("C:/Users/Tudor/Documents/CMPT-354/rating.txt");
//FileInputStream fstream = new FileInputStream("C:/Users/Tudor/Documents/CMPT-354/employees.txt");
DataInputStream dstream = new DataInputStream(fstream);
BufferedReader bf = new BufferedReader(new InputStreamReader(dstream));
String data = null;
Statement stmt;
ResultSet rs;
//Register the JDBC driver for MySQL.
Class.forName("com.mysql.jdbc.Driver");
//Define URL of database server for database named mytable on the localhos twith the default port number 3306.
String url =
"jdbc:mysql://localhost:3306/mytable";
//Get a connection to the database
Connection con =
DriverManager.getConnection(
url,"root", "turf238");
//Display URL and connection information
System.out.println("URL: " + url);
System.out.println("Connection: " + con);
//Get a Statement object
stmt = con.createStatement();
try{
stmt.executeUpdate("DROP TABLE myTable");
}catch(Exception e){
System.out.print(e);
System.out.println(
"No existing table to delete");
}//end catch
//Create a table in the database named mytable
stmt.executeUpdate("CREATE TABLE mytable(distribution char(20)," + "votes int," + "rank float," + "title char(100));");
while((data = bf.readLine()) != null){
StringTokenizer stoken = new StringTokenizer(data," ");
String distribution = stoken.nextToken();
String v = stoken.nextToken();
int votes = Integer.parseInt(v);
String r = stoken.nextToken();
float rank = Float.parseFloat(r);
String title = stoken.nextToken();
stmt = con.createStatement();
//int row = stmt.executeUpdate("INSERT into mytable VALUES('"+distribution+"' , "+votes+""+" , "+rank+",'"+title+"'" + ");");
int row = stmt.executeUpdate("INSERT into mytable VALUES('"+distribution+"',"+votes+","+rank+",'"+title+"');");
}
//Query the database, storing the result in an object of type ResultSet
rs = stmt.executeQuery("SELECT * " +
"from myTable");
//Use the methods of class ResultSet in a loop to display all of the data in the database
System.out.println("Display all results:");
while(rs.next()){
float f = rs.getInt("rank");
String t = rs.getString("title");
System.out.println("\tRank= " + f + "\tTitle = " + t);
}//end while loop
//Display the data in a specific row
//System.out.println("Display row number 2:");
//if( rs.absolute(2) ){
// int f = rs.getInt("rank");
// String t = rs.getString("title");
// System.out.println("\tRank= " + f + "\tTitle = " + t);
//}//end if statement
stmt.executeUpdate("DROP TABLE myTable"); //delete the table
con.close(); //close the connection to the database
}catch( Exception e ) {
e.printStackTrace();
}//end catch
}//end main
}
the error i get is this
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Allo')' at line 1
i have no idea how to fix my code to allow for more quotations to appear in the title and not throw off the syntax of the SQL query
Thanks for any help
Tudor