jdbc insert example from input values

Example 1: jdbc insert example from input values

// 2. Create a statement
	String sql = "insert into employees "
			+ " (last_name, first_name)" + " values (?, ?)";
	myStmt = myConn.prepareStatement(sql);
	// set param values
	myStmt.setString(1, lastName);
	myStmt.setString(2, firstName);
	// 3. Execute SQL query
	myStmt.executeUpdate();

Example 2: jdbc insert example from input values

// 0. read user input from command line: last name, first name and email
Scanner scanner = new Scanner(System.in);

System.out.print("Enter your last name: ");
String lastName = scanner.nextLine();
System.out.print("Enter your first name: ");
String firstName = scanner.nextLine();

Tags:

Java Example