import com.j256.ormlite.dao.Dao; import com.j256.ormlite.dao.DaoManager; import com.j256.ormlite.jdbc.JdbcConnectionSource; import com.j256.ormlite.support.ConnectionSource; import java.util.*; import java.sql.SQLException; public class AccountApp { // we are using a MySQl database private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/orm_lab?useUnicode=true"; private Dao accountDao; public static void main(String[] args) throws Exception { // turn our static method into an instance of Main new AccountApp().doMain(args); } private void doMain(String[] args) throws Exception { ConnectionSource connectionSource = null; try { // create our data-source for the database connectionSource = new JdbcConnectionSource(DATABASE_URL, "root", ""); // setup our DAOs setupDao(connectionSource); // read, write and delete some data boolean flag = true; while (flag) { Scanner in = new Scanner(System.in); System.out.println("What do you like to do? Create/ Read/ Update/ Delete"); String command = in.nextLine(); if (command.equalsIgnoreCase("create")) { System.out.println("Enter your username."); String name = in.nextLine(); System.out.println("Enter your password."); String password = in.nextLine(); this.createAccount(name, password); } else if (command.equalsIgnoreCase("read")) { this.readInfo(); } else if (command.equalsIgnoreCase("update")) { System.out.println("What is the id that you want to make a change to?"); int id = in.nextInt(); System.out.println("Enter your username."); String name = in.nextLine(); System.out.println("Enter your password."); String password = in.nextLine(); this.updateInfo(id, name, password); } else if (command.equalsIgnoreCase("delete")) { System.out.println("What is the id that you want to make a change to?"); int id = in.nextInt(); this.deleteInfo(id); } else if (command.equalsIgnoreCase("exit")){ flag = false; } // destroy the data source which should close underlying connections if (connectionSource != null) { connectionSource.close(); } } } catch (Exception e) { e.printStackTrace(); } } /** * Read and write some example data. */ // create, read, update, delete; private void createAccount (String name, String password) throws SQLException { Account account = new Account(name); account.setPassword(password); accountDao.create(account); //Create; } private void readInfo () throws SQLException { List accounts = accountDao.queryForAll(); for (Account acc : accounts) { System.out.print("ID" + " " + acc.getId() + " Name" + " " + acc.getName()+ "\n"); } } private void updateInfo ( int id, String name, String password) throws Exception { if (accountDao.idExists(id)) { Account account = accountDao.queryForId(id); account.setName(name); account.setPassword(password); accountDao.update(account); } } private void deleteInfo ( int id) throws Exception { if (accountDao.idExists(id)) { accountDao.deleteById(id); } } /** * Setup our DAOs */ private void setupDao (ConnectionSource connectionSource) throws Exception { accountDao = DaoManager.createDao(connectionSource, Account.class); } }