Java reads data from a file and inserts it into a database table __ database

Source: Internet
Author: User

Internship work, completed the task entrusted to the leadership, the collected data into the database, the code snippet is as follows:

Static Connection getconnection ()
				throws SQLException, IOException, classnotfoundexception
			{
				
				Properties props = new Properties ();
				FileInputStream in = new FileInputStream ("Company.ini");
				Props.load (in);
				In.close ();
				String driver=props.getproperty ("Driver");
				String url = props.getproperty ("url");
				String user = Props.getproperty ("user");
				String pass = props.getproperty ("pass");
				Load Database driver
				class.forname (driver);
				Get the database connection return
				drivermanager.getconnection (URL, user, pass);
			}

This is the code that JDBC gets the database connection, the configuration information of the database is written in the Company.ini file.

The following is the code that reads the data from the specified file and inserts it into the specified table in the database.

	Try
		{
			BufferedReader br=new bufferedreader (New FileReader ("Data.txt"));
		

		String Line=null;
		while ((Line=br.readline ())!=null)
		{
			String date=new java.text.SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"). Format (new Date ());
			String sql= "INSERT into keyword_garbage values (null, ' +line+" ', ' "+insert.keywordencode (line) +" ', 0,0, ' "+date+", NULL) ";
			Try
			{
				stmt.executeupdate (SQL);
			}
			catch (SQLException E2)
			{
				e2.printstacktrace ();
		}}} catch (filenotfoundexception E2)
		{
			e2.printstacktrace ();
		}
		catch (IOException E2)
		{
			e2.printstacktrace ();
		}
Because there is no special requirement for exception capture, I only meet the grammatical requirements. We can make further changes according to the actual situation.

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.