Thursday, 12 November 2020

Reset file reader in java

 https://stackoverflow.com/questions/34086500/how-can-i-read-the-same-file-two-times-in-java#:~:text=Close%20the%20first%20Reader%2C%20then,to%20read%20a%20second%20time.&text=You%20are%20running%20through%20the,readline)%20!%3D



try { // try to read the file 
    br = new BufferedReader(new FileReader("movies.txt"));
    String line; int numberOfMovies = 0;
    while (br.hasNextLine()){
        numberOfMovies++;
    }
    br.close();
    Movie[] movies = new Movie[numberOfMovies];
    // store in a Movie
    // array every movie of
    // the file
    String title = "";
    int id = 0;
    int likes = 0;
    int icounter = 0;
    // count to create new movie for each line
    br = new BufferedReader(new FileReader("movies.txt"));
    while ((br.hasNextLine()) {
        line = line.trim();
        line = line.replaceAll("/t", "");
        line = line.toLowerCase();
        String[] tokens = line.split(" ");
        // store every token in a
        // string array
        id = Integer.parseInt(tokens[0]);
        likes = Integer.parseInt(tokens[tokens.length]);
        for (int i = 1; i < tokens.length; i++) {
            title = title + " " + tokens[i];
        }
        movies[icounter] = new Movie(id, title, likes);
        icounter++;
    }
} catch (IOException e) { e.printStackTrace(); }

Read same file two times.

https://stackoverflow.com/questions/17269329/how-to-read-a-bufferedreader-twice-or-multiple-times/34558791#34558791
BufferedReader br = new BufferedReader(new FileReader( "D:/log_2071-04-31.txt" ));
String strLine;
ArrayList<String> ans= new ArrayList<String>();

// Read rows
while ((strLine = br.readLine()) != null) {
    System.out.println(strLine);
    ans.add(strLine);
}

// Read again
for (String result: ans) {
    System.out.println(result);
}

Sunday, 1 November 2020

procedure call with mysql

 https://www.mysqltutorial.org/calling-mysql-stored-procedures-from-jdbc/



Introducing to CallableStatement and stored procedure call syntax

To call stored procedures or stored functions in MySQL from JDBC, you use CallableStatement object, which inherits from PreparedStatement object. The general syntax of calling a stored procedure is as follows:

{?= call procedure_name(param1,param2,...)}

You wrap the stored procedure call within braces ({}). If the stored procedure returns a value, you need to add the question mark and equal (?=) before the call keyword. If a stored procedure does not return any values, you just omit the ?= sign. In case the stored procedure accepts any parameters, you list them within the opening and closing parentheses after the stored procedure’s name.

The following are examples of using the syntax for calling stored procedures in different contexts:

SyntaxStores Procedures
{  call procedure_name() }Accept no parameters and return no value
{ call procedure_name(?,?) }Accept two parameters and return no value
{?= call procedure_name() }Accept no parameter and return value
{?= call procedure_name(?) }Accept one parameter and return value