/***************************************************************************************** / Insert/Update data in a Mysql database /****************************************************************************************/ import java.sql.*; import java.util.Scanner; public class Java_write { public static void main(String[] args) { String host = "jdbc:mysql://localhost/"; //from localhost // String host = "jdbc:mysql://workshop.sps.nyu.edu/"; //from remote PC // String host = "jdbc:mysql://storm.cis.fordham.edu/"; //from remote PC String db = "demo2"; String user = "demo2"; String pswd = "demo2"; Connection connect = null; Statement stmt = null; String sql = null; Scanner input = new Scanner(System.in); System.out.print("Enter first name: "); String fname = input.nextLine(); System.out.print("Enter last name: "); String lname = input.nextLine(); System.out.print("Enter address: "); String addr = input.nextLine(); System.out.print("Enter ice cream flavor: "); String flavor = input.nextLine(); System.out.print("Enter ice cream topping: "); String top = input.nextLine(); System.out.print("Enter credit card type: "); String cc = input.nextLine(); try { Class.forName("com.mysql.cj.jdbc.Driver"); //dynamically load //the JDBC Driver class String host_db = host + db; connect = DriverManager.getConnection(host_db, user, pswd); stmt = connect.createStatement(); //create a statement object sql = "INSERT INTO cust_order " + "VALUES(0,'"+lname+"','"+fname+"','"+addr+"','"+flavor+"','"+top+"','"+cc+"',1)"; System.out.println(sql); //debugging only stmt.executeUpdate(sql); System.out.println("Update Successful"); stmt.close(); connect.close(); } catch (Exception e) { System.out.println(e); e.printStackTrace(); } } }