Advertisement
Guest User

Untitled

a guest
Jan 28th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. package Login_Registration_Project;
  2.  
  3. import java.sql.*;
  4. import java.util.Scanner;
  5. import java.lang.*;
  6.  
  7.  
  8. /**
  9.  * Created by Salek on 17.01.2017.
  10.  */
  11. public class MysqlTest {
  12.     public static void main (String[] args) {
  13.  
  14.         String url = "jdbc:mysql://localhost:3306/javabase";
  15.         String user = "java";
  16.         String passwd = "password";
  17.  
  18.         try {
  19.             //  1. Get a connection to the DB
  20.             Connection myConn = DriverManager.getConnection(url, user, passwd);
  21.             //  2. Create a statement
  22.             Statement myStmt = myConn.createStatement();
  23.             //  3. Execute SQL query
  24.  
  25.             Scanner sc = new Scanner(System.in);
  26.  
  27.             System.out.println("In order to register to the Clinic you need to create an account first, please enter your data.");
  28.             System.out.println("Last Name:");
  29.             String v1 = sc.nextLine();
  30.             System.out.println("First Name");
  31.             String v2 = sc.nextLine();
  32.             System.out.println("Address"); // This one will be problematic since nextLine() can't have spaces and addresses usually do
  33.             String v3 = sc.nextLine();
  34.             System.out.println("Email");
  35.             String v4 = sc.nextLine();
  36.             System.out.println("City");
  37.             String v5 = sc.nextLine();
  38.             System.out.println("Pesel");
  39.             long v6 = sc.nextLong();
  40.  
  41.             System.out.println("Password");
  42.             String v7 = sc.nextLine();
  43.  
  44.  
  45.             System.out.println("Login is your pesel number.");
  46.  
  47.  
  48.             int v8 = (int) (Math.random() * 10 + 100);
  49.  
  50.  
  51. //            sc.nextLine();
  52.  
  53.             String myQuery = ("insert into Patients (PatientID, LastName, FirstName, Address, Email, City, Pesel) " +
  54.                     "values ('" + v8 +  "','" + v1 +  "','" + v2 +  "','" + v3 +  "', '" + v4 +  "','" + v5 +  "','" + v6 + "')");
  55.  
  56.             String myQuery1 = ("insert into Shadow (PatientID, Password, Pesel)" +
  57.                     "values ('" + v8 + "','" + v7 + "','" + v6 + "')");
  58.  
  59.             System.out.println(myQuery);
  60.             System.out.println(myQuery1);
  61.  
  62.             myStmt.executeUpdate(myQuery);
  63.             myStmt.executeUpdate(myQuery1);
  64.  
  65.             System.out.println("Insert completed!");
  66.  
  67.         }
  68.  
  69.         catch (Exception exc) {
  70.             exc.printStackTrace();
  71.  
  72.  
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement