MySQL Insert text document and read

Source: Internet
Author: User
Tags mysql insert

1. Insert a local file into the database, the database field is saved with text

public static void Main (string[] args) {

Propkit.use ("Pro.txt");
Druidplugin Druid = new Druidplugin (Propkit.get ("Jdbcurl"), Propkit.get ("user"), Propkit.get ("password"));
Druid.start ();
Activerecordplugin arp = new Activerecordplugin (druid);
Arp.start ();
Connection conn = null;
PreparedStatement st = null;
try {
Conn= dbkit.getconfig (). getconnection ();
String sql = "INSERT into test (name) VALUES (?)";
st = conn.preparestatement (SQL);
String Path = "1.text";
File File = new file (path);
St.setcharacterstream (1, new FileReader (file), file.length ());
int num = St.executeupdate (); Perform an INSERT into the database
if (num > 0) {
System.out.println ("Insert Success");
}
} catch (Exception e) {
E.printstacktrace ();
}
}

2. Read the content from the database and save it as text to the local

public static void Main (string[] args) {
Propkit.use ("Pro.txt");
Druidplugin Druid = new Druidplugin (Propkit.get ("Jdbcurl"), Propkit.get ("user"), Propkit.get ("password"));
Druid.start ();
Activerecordplugin arp = new Activerecordplugin (druid);
Arp.start ();
Connection conn = null;
PreparedStatement st = null;
ResultSet Rs=null;
try {
Conn= dbkit.getconfig (). getconnection ();
String sql = "SELECT name from Test where id=?";
st = conn.preparestatement (SQL);
St.setint (1, 1);
rs = St.executequery (); Execute SQL statement
if (Rs.next ()) {
Reader reader = rs.getcharacterstream ("name"); Gets the item that the field is not name, which is the 1.txt file we just saved to the database
Char buffer[] = new char[1024];
int len = 0;
FileWriter out = new FileWriter ("D:\\1.txt"); Write it down to the D drive.
while (len = reader.read (buffer)) > 0) {
Out.write (buffer, 0, Len);
}
Out.close ();
Reader.close ();
}
} catch (Exception e) {
E.printstacktrace ();
}
}

MySQL Insert text document and read

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.