简体   繁体   中英

how i can view file from mysql database using java?

I have a question in java: I uploaded a file (PDF, image …etc) to the mysql database How to retrieve this? ie How I can view these files? Example: assume that uploaded pdf file to database Then in myApplication there exist a button called “View “. When clicking on it, will open PDF file. ———————————————————------------------------------------------------ I think the first step must be to download it on my computer and then use this code to open the file Desktop.getDeskTop().open(“File Path ”); That is right or not? Thank you

Let's first create a table in the mysql database, but before creating table, we need to create database first.

create database sonoo;  
use sonoo;  
create table emp(id int(10),name varchar(40),age int(3));  

sonoo is the database name, root is the username and password both.


import java.sql.*;  
class MysqlCon{  
public static void main(String args[]){  
try{  
Class.forName("com.mysql.jdbc.Driver");  
Connection con=DriverManager.getConnection(  
"jdbc:mysql://localhost:3306/sonoo","root","root");  
//here sonoo is database name, root is username and password  
Statement stmt=con.createStatement();  
ResultSet rs=stmt.executeQuery("select * from emp");  
while(rs.next())  
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
con.close();  
}catch(Exception e){ System.out.println(e);}  
}  
}  

The above example will fetch all the records of emp table.

To connect java application with the mysql database, mysqlconnector.jar file is required to be loaded.

Two ways to load the jar file

1.Paste the mysqlconnector.jar file in jre/lib/ext folder

2.Set classpath

Paste the mysqlconnector.jar file in JRE/lib/ext folder:

Download the mysqlconnector.jar file. Go to jre/lib/ext folder and paste the jar file here.

Set classpath:

There are two ways to set the classpath:

-temporary

-permanent

temporary open command prompt and write: C:>set classpath=c:\folder\mysql-connector-java-5.0.8-bin.jar;

Permanent

Go to environment variable then click on new tab. In variable name write classpath and in variable value paste the path to the mysqlconnector.jar file by appending mysqlconnector.jar;.; as C:\folder\mysql-connector-java-5.0.8-bin.jar;.;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM