Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package contactapp;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class ContactTable {
- static final String DB_URL = "jdbc:derby://localhost:1527/ContactApp";
- static final String USERNAME = "root";
- static final String PASSWORD = "root";
- private static ResultSet _rs=null;
- int currentSelectedItem;
- ContactTable()
- {
- try
- {
- Connection connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
- PreparedStatement selectAllContact = connection.prepareStatement("SELECT * FROM Contact");
- _rs = selectAllContact.executeQuery();
- }
- catch (SQLException sqlException)
- {
- sqlException.printStackTrace();
- System.exit(1);
- }
- }
- public void addContact(String firstnameTextField, String lastnameTextField, String emailTextField, String phoneTextField, String notesTextField)
- {
- try
- {
- Connection connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
- PreparedStatement insertContact = connection.prepareStatement("INSERT INTO Contact (FIRSTNAME, LASTNAME, EMAIL, PHONE, NOTES) VALUES (?,?,?,?,?)");
- insertContact.setString(1, firstnameTextField);
- insertContact.setString(2,lastnameTextField);
- insertContact.setString(3,emailTextField);
- insertContact.setString(4,phoneTextField);
- insertContact.setString(5,notesTextField);
- System.out.println(insertContact.toString());
- insertContact.executeUpdate();
- connection.commit();
- }
- catch(SQLException sqlException)
- {
- sqlException.printStackTrace();
- System.exit(2);
- }
- }
- public void updateContact(int row_id, String firstnameTextField, String lastnameTextField, String emailTextField, String phoneTextField, String notesTextField)
- {
- try
- {
- Connection connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
- PreparedStatement updateContact = connection.prepareStatement("UPDATE Contact SET FIRSTNAME = ?, LASTNAME = ?, EMAIL = ?, PHONE = ?, NOTES = ? WHERE ID = ?");
- updateContact.setString(1,firstnameTextField);
- updateContact.setString(2,lastnameTextField);
- updateContact.setString(3,emailTextField);
- updateContact.setString(4,phoneTextField);
- updateContact.setString(5,notesTextField);
- updateContact.getTableRow(6, row_id);
- updateContact.executeUpdate();
- connection.commit();
- }
- catch(SQLException sqlException)
- {
- sqlException.printStackTrace();
- System.exit(3);
- }
- }
- public void deleteContact(int row_id)
- {
- try
- {
- Connection connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
- PreparedStatement deleteContact = connection.prepareStatement("DELETE FROM Contact WHERE ID=?");
- deleteContact.setInt(1, row_id);
- deleteContact.executeUpdate();
- }
- catch(SQLException sqlException)
- {
- sqlException.printStackTrace();
- System.exit(4);
- }
- }
- public ResultSet getResults()
- {
- if (_rs != null) return _rs;
- return new ContactTable()._rs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement