Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- connect_jps.java
- package sample_jps;
- import java.sql.*;
- public class connect_jps {
- public static void main(String[] args) {
- Connection conn = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(
- "jdbc:mysql://localhost/mydb_jps","root","12345");
- System.out.println("Connection Established");
- }
- catch (Exception e) {
- System.err.println("Cannot connect to server");
- }
- finally {
- if (conn != null) {
- try {
- conn.close();
- System.out.println("Connection Terminated.");
- }
- catch (Exception e) { /* Ignore close errors */ }
- }
- }
- }
- }
- insert_jps.java
- package sample_jps;
- import java.sql.*;
- import javax.swing.*;
- public class insert_jps {
- insert_jps()
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection(
- "jdbc:mysql://localhost/my3csd_jps","root","12345");
- String studnof = JOptionPane.showInputDialog("Student#");
- String namef = JOptionPane.showInputDialog("Name");
- String sectionf = JOptionPane.showInputDialog("Section");
- String tuitionf = JOptionPane.showInputDialog("Tuition");
- String bdayf = JOptionPane.showInputDialog("Birthday");
- String sql = "INSERT INTO students VALUES("
- + "'" + studnof + "'" + ","
- + "'" + namef + "'" + ","
- +"'" + sectionf + "'" + ","
- + tuitionf + ","
- + "'" + bdayf + "'"
- + ")";
- PreparedStatement stmt = conn.prepareStatement(sql);
- stmt.executeUpdate();
- JOptionPane.showMessageDialog(null, studnof + "\nINSERTED!");
- stmt.close();
- conn.close();
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(null, e.getMessage());
- }
- }
- public static void main(String[] args)
- {
- insert_jps i = new insert_jps();
- }
- }
- View_jps.java
- package Sample_JPS;
- import java.sql.*;
- import javax.swing.JOptionPane;
- public class view_jps {
- public view_jps()
- {
- String outs = null;
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection(
- "jdbc:mysql://localhost/my3csd_jps","root","12345"
- );
- String SQL = "SELECT * FROM students";
- PreparedStatement stmt = conn.prepareStatement(SQL);
- ResultSet rs = stmt.executeQuery();
- while(rs.next())
- {
- outs = String.format(
- "%-12s %-20s %4s\t P%,10.2f\t %s",
- rs.getString("studno"),
- rs.getString("name"),
- rs.getString("section"),
- rs.getDouble("tuition"),
- rs.getDate("birthday")
- );
- System.out.println(outs);
- }
- stmt.close();
- conn.close();
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(null, e.getMessage());
- }
- }
- public static void main(String[] args) {
- view_jps v = new view_jps();
- }
- }
- Delete_jps.java
- package sample_jps;
- import java.sql.*;
- import javax.swing.*;
- public class delete_jps {
- delete_jps() {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection(
- "jdbc:mysql://localhost/my3csd_jps", "root", "12345"
- );
- String sno = JOptionPane.showInputDialog("Student Number");
- String SQL = "DELETE FROM students WHERE studno = '"+ sno + "'";
- PreparedStatement stmt = conn.prepareStatement(SQL);
- int n = stmt.executeUpdate();
- if(n != 0)
- JOptionPane.showMessageDialog(null, sno + " Deleted! ");
- else
- JOptionPane.showMessageDialog(null, sno + "NOT Found!");
- stmt.close();
- conn.close();
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(null, e.getMessage());
- }
- }
- public static void main(String[] args)
- {
- delete_jps d = new delete_jps();
- }
- }
- ModifyMethods_jps.java
- package sangoyo_employee;
- import java.sql.*;
- import javax.swing.*;
- public class modifyMethods_jps {
- public static void delete() {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection(
- "jdbc:mysql://localhost/employees_sangoyo_3csd", "root", "12345"
- );
- String emp_id = JOptionPane.showInputDialog("Employee ID");
- String SQL = "DELETE FROM employee_data_jps WHERE emp_id = '"+ emp_id + "'";
- PreparedStatement stmt = conn.prepareStatement(SQL);
- int n = stmt.executeUpdate();
- if(n != 0)
- JOptionPane.showMessageDialog(null, emp_id + " Deleted! ");
- else
- JOptionPane.showMessageDialog(null, emp_id + "NOT Found!");
- stmt.close();
- conn.close();
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(null, e.getMessage());
- }
- }
- public static void insert() {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection(
- "jdbc:mysql://localhost/employees_sangoyo_3csd","root","12345");
- String f_name = JOptionPane.showInputDialog("First Name");
- String l_name = JOptionPane.showInputDialog("Last Name");
- String title = JOptionPane.showInputDialog("Title");
- String age = JOptionPane.showInputDialog("Age");
- String yos = JOptionPane.showInputDialog("Years of Service");
- String salary = JOptionPane.showInputDialog("Salary");
- String perks = JOptionPane.showInputDialog("Perks");
- String email = JOptionPane.showInputDialog("Email");
- String sql = "INSERT INTO employee_data_jps (f_name, l_name, title, age, yos, salary, perks, email) VALUES("
- + "'" + f_name + "'" + ","
- + "'" + l_name + "'" + ","
- + "'" + title + "'" + ","
- + age + "," + yos + ","
- + salary + "," + perks + ","
- + "'" + email + "'"
- + ")";
- PreparedStatement stmt = conn.prepareStatement(sql);
- stmt.executeUpdate();
- JOptionPane.showMessageDialog(null, f_name + "\nINSERTED!");
- stmt.close();
- conn.close();
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(null, e.getMessage());
- }
- }
- public static void view() {
- String outs = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection(
- "jdbc:mysql://localhost/employees_sangoyo_3csd","root","12345"
- );
- String SQL = "SELECT * FROM employee_data_jps";
- PreparedStatement stmt = conn.prepareStatement(SQL);
- ResultSet rs = stmt.executeQuery();
- while(rs.next()) {
- outs = String.format(
- "%-4d %-20s %-20s %30s\t %2d\t %d\t P%,10.2f\t P%,10.2f\t %-30s",
- rs.getInt("emp_id"),
- rs.getString("f_name"),
- rs.getString("l_name"),
- rs.getString("title"),
- rs.getInt("age"),
- rs.getInt("yos"),
- rs.getDouble("salary"),
- rs.getDouble("perks"),
- rs.getString("email")
- );
- System.out.println(outs);
- }
- stmt.close();
- conn.close();
- }
- catch(Exception e) {
- JOptionPane.showMessageDialog(null, e.getMessage());
- }
- }
- }
- mySQLscript_JPS.sql
- USE my3CSD_jps;
- DROP TABLE IF EXISTS students;
- DROP TABLE IF EXISTS subjects;
- DROP TABLE IF EXISTS enroll;
- CREATE TABLE students(
- studno VARCHAR(10) NOT NULL,
- name VARCHAR(30),
- section VARCHAR(4),
- tuition DOUBLE(10,2),
- birthday DATE DEFAULT '0000-00-00',
- PRIMARY KEY (studno)
- );
- CREATE TABLE subjects(
- subjcode VARCHAR(5) NOT NULL,
- title VARCHAR(25) NOT NULL,
- units INTEGER(1),
- PRIMARY KEY (subjcode)
- );
- CREATE TABLE enroll(
- studno VARCHAR(10) NOT NULL,
- subjcode VARCHAR(5) NOT NULL
- );
- INSERT INTO students VALUES
- ('2007008208','Susan Estanislao','3CSD',38000.00,'1990-08-20'),
- ('2007012345','Jess Agbayani','3CSA',35000.25,'1990-12-12'),
- ('2007001234','Cedric Manahan','3ISA',40000.50,'1999-01-31'),
- ('2007001122','Devra Galleto','3ISB',45000.75,'1995-09-20'),
- ('2007000011','Chlowee Cubangbang','3ISD',42000.50,'1990-11-20'),
- ('2007001212','Deo Agbayani','3ITE',38000.00,'1990-01-20'),
- ('2008006600','Jerson Sangoyo','3CSD',44000.00,'1990-10-23');
- INSERT INTO subjects VALUES
- ('CS113','Unix Operating System',1),
- ('IM201','Database Management',3),
- ('IT103','Computer Organization',3),
- ('ICS3L','Java Programming Lab',1);
- INSERT INTO enroll VALUES
- ('2007008208','ICS3L'),
- ('2007008208','CS113'),
- ('2007008208','IM201'),
- ('2007012345','CS113'),
- ('2007012345','IM201'),
- ('2007012345','IT103'),
- ('2007012345','ICS3L'),
- ('2007001234','CS113'),
- ('2007001234','ICS3L'),
- ('2007001234','IT103'),
- ('2007001122','CS113'),
- ('2007001122','IM201'),
- ('2007001122','IT103'),
- ('2007000011','IM201'),
- ('2007000011','CS113'),
- ('2007001212','CS113'),
- ('2007001212','ICS3L'),
- ('2008006600','IM201');
- ('2008006600','CS113');
- ('2008006600','IT103');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement