Advertisement
Protorianbz

Userdata-MySql

Sep 22nd, 2020
2,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.66 KB | None | 0 0
  1. package database.newpackage;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7. import java.util.Scanner;
  8.  
  9. public class Imported {
  10.  
  11.     public static void main(String[] args) {
  12.            
  13.             Scanner keyboard = new Scanner(System.in);  //setting keyborad input
  14.            
  15.         String dbURL = "jdbc:mysql://localhost:3306/hello";
  16.         String username = "root";
  17.         String password = "";                
  18.                                
  19.                 int i = 0;
  20.                 int j = i+1;
  21.                 int records = 0;
  22.            
  23.                     System.out.println("How many new records do you "
  24.                             + "wish to enter : ");
  25.                    
  26.                         records = keyboard.nextInt();
  27.                                                    
  28.                 String [] fname = new String [records];
  29.                 String [] lname = new String [records];                
  30.                 String [] email = new String [records];
  31.                 String [] phone = new String [records];
  32.                 String [] ssn = new String [records];
  33.                 String [] uname = new String [records];
  34.                 String [] pword = new String [records];
  35.                 int [] age = new int [records];
  36.                 String [] address = new String [records];
  37.                 String [] location = new String [records];
  38.                
  39.                
  40.                 // DATA GATHERING
  41.                
  42.             for (i = 0; i < records; i ++) {
  43.                      
  44.                 // Workaround for fname line input skip
  45.                     fname[i] = keyboard.nextLine();
  46.  
  47.                 // 1    
  48.                     System.out.println("Please enter the " + j
  49.                             + " new users First Name : ");
  50.                         fname[i] = keyboard.nextLine();                        
  51.                 // 2
  52.                     System.out.println("Please enter the " + j
  53.                             + " new users Last Name : ");
  54.                         lname[i] = keyboard.nextLine();                    
  55.                 //3    
  56.                     System.out.println("Please enter the " + j
  57.                             + "  new users Email Address : ");
  58.                         email[i] = keyboard.nextLine();  
  59.                 //4        
  60.                     System.out.println("Please enter the " + j
  61.                             + "  new users Phone Number : ");
  62.                         phone[i] = keyboard.nextLine();
  63.                 //5        
  64.                     System.out.println("Please enter the " + j
  65.                             + "  new users Social Security Number : ");
  66.                         ssn[i] = keyboard.nextLine();
  67.                 //6        
  68.                     System.out.println("Please enter the " + j
  69.                             + "  new users Username : ");
  70.                         uname[i] = keyboard.nextLine();
  71.                 //7        
  72.                     System.out.println("Please enter the " + j
  73.                             + "  new users Password : ");
  74.                         pword[i] = keyboard.nextLine();
  75.                 //8        
  76.                     System.out.println("Please enter the " + j
  77.                             + "  new users Age : ");
  78.                         age[i] = keyboard.nextInt();
  79.                 //9        
  80.                     System.out.println("Please enter the " + j
  81.                             + "  new users Address : ");
  82.                         address[i] = keyboard.nextLine();
  83.                        
  84.                         //Workaround to accept address information
  85.                         address[i] = keyboard.nextLine();
  86.                 //10
  87.                     System.out.println("Please enter the " + j
  88.                             + "  new users Location : ");
  89.  
  90.                     // Workaround for location line input skip
  91.                     location[i] = keyboard.nextLine();  // workaround
  92. //                        location[i] = keyboard.nextLine();  
  93.                        
  94.                     System.out.println("Information for record "
  95.                             + records + " inputted");
  96.                    
  97.                     System.out.println(); System.out.println();
  98.             }    
  99.  
  100.             try (Connection conn = DriverManager.getConnection(dbURL,
  101.                     username, password)) {                
  102.                 for (i = 0; i < records; i++ ) {
  103.             String sql = "INSERT INTO users (username, password, "
  104.                                 + "fname, lname, email, phone, ssn, age, "
  105.                                 + "address, location) VALUES ( ?, ?, ?, ?, ?, "
  106.                                 + "?, ?, ?, ?, ?)";
  107.  
  108.             PreparedStatement statement = conn.prepareStatement(sql);
  109.             statement.setString(1, uname[i]);
  110.             statement.setString(2, pword[i]);
  111.             statement.setString(3, fname[i]);
  112.                         statement.setString(4, lname[i]);
  113.             statement.setString(5, email[i]);
  114.             statement.setString(6, phone[i]);
  115.             statement.setString(7, ssn[i]);
  116.                         statement.setInt(8, age[i]);
  117.             statement.setString(9, address[i]);                        
  118.             statement.setString(10, location[i]);                        
  119.                        
  120.             int rowsInserted = statement.executeUpdate();
  121.                
  122.                         if (rowsInserted > 0) {
  123.                 System.out.println("A new record have been "
  124.                                         + "inserted successfully!");
  125.                                 System.out.println("First Name : " + fname[i]
  126.                                         + " " + lname[i]);
  127.                                 System.out.println("Email : " + email[i]);
  128.                                 System.out.println("Phone Number : "
  129.                                         + phone[i]);
  130.                                 System.out.println("Username : " + uname[i]);
  131.                                 System.out.println("Password : " + pword[i]);
  132.                                 System.out.println("Username : " + uname[i]);
  133.                                 System.out.println("Social Number : " + ssn[i]);
  134.                                 System.out.println("Address : " + address[i]);
  135.                                 System.out.println("Location : " + location[i]);
  136.                         }
  137.                 }  
  138.         System.out.println("!! CONGRATS !!! a total of " + j +
  139.                         " new records have been added!");
  140.                
  141.                 }
  142.                 catch (SQLException ex) {
  143.                         ex.printStackTrace();
  144.         }      
  145.     }
  146.        
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement