Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package database.newpackage;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.Scanner;
- public class Imported {
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in); //setting keyborad input
- String dbURL = "jdbc:mysql://localhost:3306/hello";
- String username = "root";
- String password = "";
- int i = 0;
- int j = i+1;
- int records = 0;
- System.out.println("How many new records do you "
- + "wish to enter : ");
- records = keyboard.nextInt();
- String [] fname = new String [records];
- String [] lname = new String [records];
- String [] email = new String [records];
- String [] phone = new String [records];
- String [] ssn = new String [records];
- String [] uname = new String [records];
- String [] pword = new String [records];
- int [] age = new int [records];
- String [] address = new String [records];
- String [] location = new String [records];
- // DATA GATHERING
- for (i = 0; i < records; i ++) {
- // Workaround for fname line input skip
- fname[i] = keyboard.nextLine();
- // 1
- System.out.println("Please enter the " + j
- + " new users First Name : ");
- fname[i] = keyboard.nextLine();
- // 2
- System.out.println("Please enter the " + j
- + " new users Last Name : ");
- lname[i] = keyboard.nextLine();
- //3
- System.out.println("Please enter the " + j
- + " new users Email Address : ");
- email[i] = keyboard.nextLine();
- //4
- System.out.println("Please enter the " + j
- + " new users Phone Number : ");
- phone[i] = keyboard.nextLine();
- //5
- System.out.println("Please enter the " + j
- + " new users Social Security Number : ");
- ssn[i] = keyboard.nextLine();
- //6
- System.out.println("Please enter the " + j
- + " new users Username : ");
- uname[i] = keyboard.nextLine();
- //7
- System.out.println("Please enter the " + j
- + " new users Password : ");
- pword[i] = keyboard.nextLine();
- //8
- System.out.println("Please enter the " + j
- + " new users Age : ");
- age[i] = keyboard.nextInt();
- //9
- System.out.println("Please enter the " + j
- + " new users Address : ");
- address[i] = keyboard.nextLine();
- //Workaround to accept address information
- address[i] = keyboard.nextLine();
- //10
- System.out.println("Please enter the " + j
- + " new users Location : ");
- // Workaround for location line input skip
- location[i] = keyboard.nextLine(); // workaround
- // location[i] = keyboard.nextLine();
- System.out.println("Information for record "
- + records + " inputted");
- System.out.println(); System.out.println();
- }
- try (Connection conn = DriverManager.getConnection(dbURL,
- username, password)) {
- for (i = 0; i < records; i++ ) {
- String sql = "INSERT INTO users (username, password, "
- + "fname, lname, email, phone, ssn, age, "
- + "address, location) VALUES ( ?, ?, ?, ?, ?, "
- + "?, ?, ?, ?, ?)";
- PreparedStatement statement = conn.prepareStatement(sql);
- statement.setString(1, uname[i]);
- statement.setString(2, pword[i]);
- statement.setString(3, fname[i]);
- statement.setString(4, lname[i]);
- statement.setString(5, email[i]);
- statement.setString(6, phone[i]);
- statement.setString(7, ssn[i]);
- statement.setInt(8, age[i]);
- statement.setString(9, address[i]);
- statement.setString(10, location[i]);
- int rowsInserted = statement.executeUpdate();
- if (rowsInserted > 0) {
- System.out.println("A new record have been "
- + "inserted successfully!");
- System.out.println("First Name : " + fname[i]
- + " " + lname[i]);
- System.out.println("Email : " + email[i]);
- System.out.println("Phone Number : "
- + phone[i]);
- System.out.println("Username : " + uname[i]);
- System.out.println("Password : " + pword[i]);
- System.out.println("Username : " + uname[i]);
- System.out.println("Social Number : " + ssn[i]);
- System.out.println("Address : " + address[i]);
- System.out.println("Location : " + location[i]);
- }
- }
- System.out.println("!! CONGRATS !!! a total of " + j +
- " new records have been added!");
- }
- catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement