Skip to content

Instantly share code, notes, and snippets.

@shade34321
Last active December 14, 2015 13:58
Show Gist options
  • Save shade34321/5096733 to your computer and use it in GitHub Desktop.
Save shade34321/5096733 to your computer and use it in GitHub Desktop.
Java with Derby
import java.sql.*;
import java.util.Scanner;
public class Employee {
private static int empolyeeNumber = 1;
private int age;
private String first, last, phone, tableName, serverName, dbName;
private Connection con = null;
private Statement stm = null;
private ResultSet res = null;
private PreparedStatement insertEmployee = null;
private PreparedStatement searchEmployee = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee e = new Employee();
}
public Employee(){
serverName = "localhost";
dbName = "EmployeeDB";
tableName = "Employee";
connect();
//String insert = "insert into EmployeeDB(ID_number, firstName, lastName, phoneNumber, age) VALUES ( ?, ?, ?, ?. ?)";
//String search = "select lastName, firstName from EmployeeDB where lastName = ? and firstName = ?";
try {
stm = con.createStatement();
//insertEmployee = con.prepareStatement(insert);
//searchEmployee = con.prepareStatement(search);
inputData();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void inputData(){
Scanner in = new Scanner(System.in);
in.useDelimiter(System.getProperty("line.separator"));
String cont = "yes", sql;
boolean test;
do{
System.out.println("Please enter the Employee's first name:");
first = in.next();
System.out.println("Please enter the Employee's last name:");
last = in.next();
System.out.println("Please enter the Employee's phone number:");
phone = in.next();
System.out.println("Please enter the Employee's age:");
age = in.nextInt();
try {
res = stm.executeQuery("select lastName, firstName from " + tableName);
//searchEmployee.setString(1, last);
//searchEmployee.setString(2, first);
//res = searchEmployee.executeQuery();
test = true;
while(res.next()){
if((res.getString("lastName").equals(last) && res.getString("firstName").equals(first))){
System.out.println("You already added " + first + " " + last);
test = false;
}
}
if(test){
//insertEmployee.setInt(1, empolyeeNumber);
//insertEmployee.setString(2, first);
//insertEmployee.setString(3, last);
//insertEmployee.setString(4, phone);
//insertEmployee.setInt(5, age);
//insertEmployee.executeUpdate();
sql = "insert into Employee values (" + empolyeeNumber + ", '" + first + "' , '" + last + "' , '" +phone + "' ," +age + ")";
stm.executeUpdate(sql);
System.out.println("You successfully added " + first + " " + last);
empolyeeNumber++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Would you like to continue? (yes or no)");
cont = in.next();
test = ((cont == "yes" || cont == "Yes" || cont == "y" || cont == "Y"));
}while((cont == "yes" || cont == "Yes" || cont == "y" || cont == "Y"));
try {
res = stm.executeQuery("select * from Employee");
System.out.println("ID Number First Name Last Name Phone Number Age");
while(res.next()){
System.out.println(res.getInt("ID_number") + " " + res.getString("firstName") + " " + res.getString("lastName") + " " + res.getString("phoneNumber") + " " + res.getInt("age"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
close();
}
private void connect(){
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
} catch (Exception e) {
System.out.println("No DB Drivers!");
e.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:derby://localhost:1527/EmployeeDB");
} catch (Exception e) {
System.out.println("No DB connection!");
e.printStackTrace();
}
}
public void close(){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.jdbc.ClientDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Employee.connect(Employee.java:91)
at Employee.<init>(Employee.java:26)
at Employee.main(Employee.java:18)
Caused by: org.apache.derby.client.am.DisconnectException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
at org.apache.derby.client.net.NetAgent.<init>(Unknown Source)
at org.apache.derby.client.net.NetConnection.newAgent_(Unknown Source)
at org.apache.derby.client.am.Connection.<init>(Unknown Source)
at org.apache.derby.client.net.NetConnection.<init>(Unknown Source)
at org.apache.derby.client.net.NetConnection40.<init>(Unknown Source)
at org.apache.derby.client.net.ClientJDBCObjectFactoryImpl40.newNetConnection(Unknown Source)
... 6 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at javax.net.DefaultSocketFactory.createSocket(Unknown Source)
at org.apache.derby.client.net.OpenSocketAction.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 12 more
Exception in thread "main" java.lang.NullPointerException
at Employee.<init>(Employee.java:29)
at Employee.main(Employee.java:18)
No DB connection!
public class main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee e = new Employee();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment