954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pasing texte file to create mysql database using java

Hi to all

I need some help to write a java program to parse 2 text file one containing the details of the database and the other the data that will be used to insert in the tables of the database

A sample for the first file :

schema : Test
Table : user(id#int;name#string;mail#string)
Table : students(id#int;name#string;class#string)

second file :

user
1;peter;peter@mail.com
2;micheal;micheal@email.com
3;george;george@yahoo.fr
students
1;jhon;CMP1
2;piere;CMP3
3;sabrina;CM4

thanks

ktunisia
Newbie Poster
5 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 
peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

Search for code examples on how to read files (Scanner, BufferedReader). Write some test classes that just print the data of the files
Search for code examples on how to run queries. Write some test classes that execute simple.
Use the above to read the data, construct the queries and execute them

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

hi, i need ony some helps ???
ok i will explain what i have done

i already rote a class that can read a texte file using readline
and one other that can create database , tables.

but i have problems in how can join all together

ktunisia
Newbie Poster
5 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

What you have read from one file, store it in variables and use those to create the query.

Then read the other file with the data and for each line, generate the query with the values, and pass it as parameter to a method that executes it

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

What you have read from one file, store it in variables and use those to create the query.

Then read the other file with the data and for each line, generate the query with the values, and pass it as parameter to a method that executes it

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

Sorry for the double post. Internet connection problems

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

hi, i need ony some helps ??? ok i will explain what i have done

i already rote a class that can read a texte file using readline and one other that can create database , tables.

but i have problems in how can join all together


That was my request, provide your code.
We do not need verbal explanation of what is your issue in hypothetical code, we need real code and description of problem with it

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
That was my request, provide your code. We do not need verbal explanation of what is your issue in hypothetical code, we need real code and description of problem with it

ok your right and i'm so sorry, this is my first post

here the source

the first one is LireFichier it can read thefile "CheminFichier"
import java.io.*;

import dbPKG.*;
class LireFichier
{

public LireFichier(String CheminFichier)
{
try{

FileInputStream fstream = new FileInputStream(CheminFichier);

DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;

while ((strLine = br.readLine()) != null) {


System.out.println (strLine);

}

in.close();

}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}


the second one is CreateDataBase

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateDataBase wich create the database "nomBase"
{
public CreateDataBase(String nomBase)
{

Connection connection = null;
Statement statement = null;
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url = "jdbc:mysql://localhost/mysql";
connection = DriverManager.getConnection(url, "root", "");

statement = connection.createStatement();
String reqSql = "CREATE DATABASE "+ nomBase;
statement.executeUpdate(reqSql);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
} // nothing we can do
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
} // nothing we can do
}
}
}

}

and the last one is CreateTable that create table on the database "nomBase"
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateTable
{
public CreateTable(String nomBase )
{

Connection connection = null;
Statement statement = null;
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url = "jdbc:mysql://localhost/mysql";
connection = DriverManager.getConnection(url, "root", "");

statement = connection.createStatement();
String reqSql = "create table "+nomBase+".COFFEES " +
"(COF_NAME VARCHAR(32), " +
"SUP_ID INTEGER, " +
"PRICE FLOAT, " +
"SALES INTEGER, " +
"TOTAL INTEGER)";

statement.executeUpdate(reqSql);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
} // nothing we can do
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
} // nothing we can do
}
}
}

}

My probleme is
can I use arrylist to store the values from the text file and then execute the two other classes
or is there a best way to join all this peaces to do the necessary

i hope i was clear

ktunisia
Newbie Poster
5 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

who could kindly check the code for me?

ktunisia
Newbie Poster
5 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You