19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

copyFile();<br />

}<br />

catch (Exception ex) {<br />

jlblStatus.setText(ex.<strong>to</strong>String());<br />

}<br />

}<br />

});<br />

/** Display the file in the text area */<br />

private void showFile() {<br />

Scanner input = null;<br />

try {<br />

// Use a Scanner <strong>to</strong> read text from the file<br />

input = new Scanner(new File(jtfFilename.getText().trim()));<br />

}<br />

// Read a line and append the line <strong>to</strong> the text area<br />

while (input.hasNext())<br />

jtaFile.append(input.nextLine() + '\n');<br />

}<br />

catch (FileNotFoundException ex) {<br />

System.out.println("File not found: " + jtfFilename.getText());<br />

}<br />

catch (IOException ex) {<br />

ex.printStackTrace();<br />

}<br />

finally {<br />

if (input != null) input.close();<br />

}<br />

private void copyFile() throws Exception {<br />

// Load the JDBC driver<br />

Class.forName(((String)jcboDriver.getSelectedItem()).trim());<br />

System.out.println("Driver loaded");<br />

}<br />

// Establish a connection<br />

Connection conn = DriverManager.getConnection<br />

(((String)jcboURL.getSelectedItem()).trim(),<br />

jtfUsername.getText().trim(),<br />

String.valueOf(jtfPassword.getPassword()).trim());<br />

System.out.println("Database connected");<br />

// Read each line from the text file and insert it <strong>to</strong> the table<br />

insertRows(conn);<br />

private void insertRows(Connection connection) {<br />

// Build the SQL INSERT statement<br />

String sqlInsert = "insert in<strong>to</strong> " + jtfTableName.getText()<br />

+ " values (";<br />

// Use a Scanner <strong>to</strong> read text from the file<br />

Scanner input = null;<br />

// Get file name from the text field<br />

String filename = jtfFilename.getText().trim();<br />

try {<br />

// Create a scanner<br />

10

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!