0% found this document useful (0 votes)
22 views

Computer 9 Javascript Display - Java

The document contains code for adding a new record to a database table. It prompts the user to enter information like name, address, phone number, and birthday. It then inserts the record into the profile table if it doesn't already exist.

Uploaded by

daimyfreesia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Computer 9 Javascript Display - Java

The document contains code for adding a new record to a database table. It prompts the user to enter information like name, address, phone number, and birthday. It then inserts the record into the profile table if it doesn't already exist.

Uploaded by

daimyfreesia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Import java.io.

*;
Import java.sql.*;

public class Insert {


public static void main(String arg[]) {
Connection connection=null;
try {
Class.forname(“com.mariadb.jdbc.Driver”);
String = url “jdbc:mariadb://localhost/friends?
zeroDateTimeBehavior=convertToNulll”;
connection = DriverManager.getConnection(url, “peter”, “peter”);
}
catch ( Exception x ) {
x.printStackTrace();
}

String lastname = null, firstname = null, middleinit = null, address = null, landline = null;
String cellphone = null, email = null, bday =null, comments = null, favorite dish = null;
Date birthday = new Date (0000-00-00);
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));

Try {
System.out.println(“You have chosen to add a record”);
Sysytem.out.println(“Please enter your friend’s basic info.”);
System.out.print(“First name : “);
firstname = br.readLine();
System.out.print(“Middle initial : “);
middlinit = br.readLine();
System.out.print(“Lastname : “);
lastname = br.readLine();
String qry = “SELECT * from profile where firstname=? and mi=? and lastname=?’;
ps.setString(1,firstname);
ps.setString(2,middleinit);
ps.setString(3,lastname);
ps.excecuteQuery();
ResultSet resset = ps.getResultSet();
If (resset).next()) {
System.out.println(“\nOops, that record is already in the database! “);
System.out.println(“Sorry, you can’t add an existing record”);
System.exit(0);
}

else {
System.out.print(“Address : “);
address = br.readLine();
System.out.print(“Land line : “);
landline = br.readLine();
System.out.print(“Cellphone : “);
cellphone = br.readLine();
System.out.print(“Email : “);
email = br.readLine();
System.out.print(“Bday (yyyy-mm-dd) : “);
bday = br.readLine();
System.out.print(“Comments : “);
comments = br.readLine();
System.out.print(“Favorite Dish : “);
favoritedish = br.readLine();
String query = “INSERT INTO profile ( ID, lastname, firstname, mi, address, landline,
cellphone, email, birthday, comments, favoritedish)
VALUES
“(?,?,?,?,?,?,?,?,?,?,?)”;
prepareStatement prepstmt = connection.prepareStatement (query);
preps.setInt(1,0);
prepstmt.setString(2,lastname);
prepstmt.setString(3,firstname);
prepstmt.setString(4,middleinit);
prepstmt.setString(5,address);
prepstmt.setString(6,landline);
prepstmt.setString(7,cellphone);
prepstmt.setString(8,email);
prepstmt.setDate(9, birthday.valueOf (bday));
prepstmt.setString(10, comments);
prepstmt.setString(11, favoritedish);
int row = prepstmt.executeUpdate();
System.out.println(“Thankyou for inputting the record of “ + firstname + “ “ + lastname);
System.out.println(row + “ record added”);
}
}
catch (IOException e) {
System.out.println(“I/O Error!”);
System.exit(1);
}
catch (SQLException ex) {
System.err.println(“SQLException: “ + ex.getMessage());
}
catch (IllegalArguementException err) {
System.out.println(“You have a wrong date format for the birthday!”)
System.out.println(“You should use the format yyyy-mm-dd. Ex. 1990-03-25”);
System.out.println(“Sorry, this record cannot be added now.”);
System.exit(1);
}
)
)

You might also like