D
D
DocHarly2014-03-10 21:46:15
PHP
DocHarly, 2014-03-10 21:46:15

Java: How to access file from MySQL DB

It is necessary to access the file from the MySQL database in order to modify it later, then save these changes. Please help, I'm new to Java.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
v_prom, 2014-03-11
@DocHarly

an example of how to work with the database:

package news_parser_connection;

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

public class LocalhostDBConnection {

  public static void main(String args[]) {
    Connection connection;
    try {
      // Название драйвера
      String driverName = "com.mysql.jdbc.Driver"; 

      Class.forName(driverName);

      // Create a connection to the database
      String serverName = "localhost";
      String mydatabase = "db_name";
      String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
      String username = "root";
      String password = "root";

      connection = DriverManager.getConnection(url, username, password);
      System.out.println("is connect to DB" + connection);

      String query = "Select * FROM news";
      Statement stmt = connection.createStatement();

      ResultSet rs = stmt.execute(query);
      String dbtime;
      while (rs.next()) {
        dbtime = rs.getString(1);
        System.out.println(dbtime);
      } // end while

      connection.close();
    } // end try
    catch (ClassNotFoundException e) {
      e.printStackTrace();
      // Could not find the database driver
    } catch (SQLException e) {
      e.printStackTrace();
      // Could not connect to the database
    }
  }
}

The easiest way is to save a link to the file in the database, get it with a request from the database and already work with the file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question