Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. package softwareEng;
  2.  
  3. import java.sql.*;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.Scanner;
  9.  
  10. import javax.swing.JOptionPane;
  11.  
  12. public class driver {
  13.  
  14.     public static void main(String[] args) throws IOException {
  15.        
  16.         // Build database
  17.         String userName = JOptionPane.showInputDialog("Type in the username for your MySql database");
  18.         String passWord = JOptionPane.showInputDialog("Type in the password for your MySql databse");
  19.         buildDatabase(args[0], args[1], userName, passWord);
  20.         Menu gui = new Menu(args[1], userName, passWord);
  21.        
  22.     } // ends main
  23.  
  24.    
  25.     public static void buildDatabase(String inFile, String outFile, String userName, String passWord) throws IOException {
  26.        
  27.         Scanner inFile_1 = new Scanner(new FileReader(inFile));
  28.         FileWriter outFile_1 = new FileWriter(outFile);
  29.        
  30.        
  31.         try {
  32.            
  33.              String driverName = "org.gjt.mm.mysql.Driver";
  34.              Class.forName(driverName);
  35.              java.sql.Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project", userName, passWord);
  36.            
  37.              DatabaseMetaData metadata = myConn.getMetaData();
  38.              ResultSet rs = metadata.getTables(null,null,"Pets",null);
  39.              if (!rs.next()) {
  40.              
  41.                  while (inFile_1.hasNextLine()) {
  42.              
  43.                     java.sql.Statement myStmt = myConn.createStatement();
  44.                     String input = inFile_1.nextLine();
  45.                     outFile_1.write(input);
  46.                     outFile_1.write('\n');
  47.                     myStmt.executeUpdate(input);
  48.              
  49.                 }
  50.              
  51.              } // if table Pets doesnt exist, create it using input
  52.        
  53.              
  54.         } catch (Exception exc) {
  55.            
  56.             JOptionPane.showMessageDialog(null, "Invalid Login");
  57.             System.exit(0);
  58.         }
  59.        
  60.        
  61.         inFile_1.close();
  62.         outFile_1.close();
  63.        
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement