Friday, December 18, 2009

Its beginning to look a lot like Christmas!

Yes, school is done, work is done (til Janurary), and were going home in 2 days!! That means instead of thinking about all of the crazy school work I have to get done, I can start thinking more about Christmas!! I am so excited and happy that I get to see my family in two days! My mom called today and said that they are suppose to get a huge snow storm starting tonight sometime and its suppoe to snow 14-20 inches between now and Sunday!! Thats crazy, Maryland and VA hasn't gotten that much snow for so long! I hope it doesn't affect our traveling. We leave Sunday morning at 10am and I want to get into Maryland at the intented time 4pm! Last Christmas we got stuck in Las Vegas for ever because their was a lot of rain in California and thats where our plane was coming from! It took forever to get home. I don't want that to happen again. So we will be praying that the snow doesn't affect our travels!!!

I am so happy that I am done with all my classes. I really enjoyed them all this semester. I learned a lot and grew a lot too, but I was getting a little tired of assignment after assignment and test after test! It was time for a break, and this break is much needed. All I can think about is relaxing at home with family and friends who we haven't seen in over a year!! It will be too bad that my oldest sister and her husband wont be able to come up due to their son coming in a few weeks. But we still love them and will miss them very much. I know we will be thinking about them for sure!!!

So only one more semester for Jordan and I! YES that means graduation is almost here and freedom. haha I don't know why I think that life is going to get easier once we finish school! I am just very excited to move to a new place and start somewhere else a new chapeter. Jordan is still on the search for internships. He has been doing a lot of applications and he hopes to stop by some places in DC when we are home to talk to them personally! Hopefully that will get him in somewhere!

Well thats all for now, I will be updating soon so I can tell everyone about our trip and how much fun we have. Ill take pictures too, I always like reading blogs when there are pictures to look at. Its so much fun!! okay unitl next time!!!

Wednesday, December 16, 2009

mySQL

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

public class Customer {
Connection con = null;
ResultSet rs;
PreparedStatement stmt;
public static void main(String[] args) {
Customer cust = new Customer();
cust.OpenConn();
cust.dropTable();
cust.insertValues();
cust.updateValues();
cust.CloseConnection();

}
public void OpenConn() {

String url = "jdbc:mysql://localhost:3306/sampledb";

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();

con = DriverManager.getConnection(url, "student", "student");

if(!con.isClosed())
System.out.println("Successfully connected to " +
"MySQL server using TCP/IP...");

System.out.println("URL: " + url);
System.out.println("Connection: " + con);
}
catch (Exception e) {
System.err.print(e.getMessage());
System.out.println("You are not connect");
}
}

public void dropTable() {
try {
stmt = con.prepareStatement("DROP TABLE IF EXISTS Customer");
stmt.executeUpdate();
String mysql = ("CREATE TABLE Customer(customer_id int unsigned primary key auto_increment," +
"first_name varchar(30) not null" +
",last_name varchar(30) not null" +
",email varchar(40) not null" +
",phone varchar(12) not null" +
",street_address varchar(30) not null" +
",city varchar(30) not null" +
",state varchar(20) not null" +
",zip varchar(10) not null)");
stmt = con.prepareStatement(mysql);
stmt.executeUpdate();
}
catch (Exception e) {
System.out.print(e);
System.out.println("No existing table to delete");
}
}

public void insertValues() {
int num = 1;
while (num == 1) {
try {

ArrayList arr = new ArrayList();
String first_name = JOptionPane.showInputDialog(null, "Please Input a First Name");
arr.add(first_name);
String last_name = JOptionPane.showInputDialog(null, "Please Input a Last_Name");
arr.add(last_name);
String email = JOptionPane.showInputDialog(null, "Please Input an email address");
arr.add(email);
String phone = JOptionPane.showInputDialog(null, "Please Input a Telephone Number");
arr.add(phone);
String street_address = JOptionPane.showInputDialog(null, "Please Input a Street Address");
arr.add(street_address);
String city = JOptionPane.showInputDialog(null, "Please Input a City");
arr.add(city);
String state = JOptionPane.showInputDialog(null, "Please Input a State");
arr.add(state);
String zip = JOptionPane.showInputDialog(null, "Please Input a zip");
arr.add(zip);
String more = JOptionPane.showInputDialog(null, "Do you want to add another customer? (1 for yes or 2 for no)");
num = Integer.parseInt(more);

stmt = con.prepareStatement("INSERT INTO Customer VALUES(null,?,?,?,?,?,?,?,?)");
int j = 0;
for (int i = 1; i <= arr.size(); i++) {
stmt.setString(i, (String) arr.get(j));
j++;
}
stmt.executeUpdate();

rs = stmt.executeQuery("SELECT * " +
"from Customer");

System.out.println("Display all results:");

while(rs.next()){
String id = rs.getString("customer_id");
String first = rs.getString("first_name");
String last = rs.getString("last_name");
System.out.println("\tcustomer id = " + id
+ "\n\tfirst name = " + first
+ "\n\tlast name = " + last);
}
}
catch (Exception e) {
System.err.print(e.getMessage());
}
}

}

public void updateValues() {
int num = 1;
String update = JOptionPane.showInputDialog(null, "Do you want to update a last name? (1 for yes, 2 for no)");
num = Integer.parseInt(update);

while (num == 1) {
try {
ArrayList arr = new ArrayList();
String last_name = JOptionPane.showInputDialog(null, "Please enter last_name");
arr.add(last_name);
String email = JOptionPane.showInputDialog(null, "Please enter email");
arr.add(email);
String updateAgain = JOptionPane.showInputDialog(null, "Do you want to update another customer? (1 for yes, 2 for no)");
num = Integer.parseInt(updateAgain);
stmt = con.prepareStatement("UPDATE Customer SET " +
"last_name = ? " +
"WHERE email = ?");
int j = 0;
for (int i = 1; i <= arr.size(); i++) {
stmt.setString(i, (String) arr.get(j));
j++;
}

stmt.executeUpdate();

}
catch (Exception e) {
System.err.print(e.getMessage());
}
}
}

public void CloseConnection() {
try {
if(con != null)
con.close();
}
catch(SQLException e) {
System.err.print(e.getMessage());
}
}
}

answerOne

package application;

import java.util.ArrayList;
import java.util.Scanner;

public class AnswerOne implements Command{

@Override
public void invoke(ArrayList Paramaters) {
System.out.println("Yeah, you bet!");
}
}

command

package application;

import java.util.ArrayList;


public interface Command {
public void invoke(ArrayList paramaters);
}

Controller

package application;


import java.util.ArrayList;
import java.util.HashMap;

public class Controller {
HashMap map = new HashMap();
public Controller(){
map.put("Is there homework in this class?", "application.AnswerOne");
map.put("Where can I find more information about that?", "application.AnswerTwo");
map.put("How can I avoid those ‘Oh, crap!’ moments?", "application.AnswerThree");
}



public void handleRequest(String question, ArrayList parameters){

String className = (String) map.get(question);
try {
Class aClass = Class.forName(className);
Command aCmdObject = (Command) aClass.newInstance();
aCmdObject.invoke(parameters);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

SomeApplication

package application;

import java.util.ArrayList;
import java.util.Scanner;

public class SomeAplication {
public static void main(String[] args){
Controller test = new Controller();

while(true) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a question");
String question = input.nextLine();
test.handleRequest(question, new ArrayList());
}
}
}

User

package edu.byui.examples;

import java.util.List;

import java.util.Set;

import javax.persistence.CascadeType;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.GeneratedValue;

import javax.persistence.JoinColumn;

import javax.persistence.JoinTable;

import javax.persistence.ManyToMany;

import javax.persistence.OneToMany;

import javax.persistence.Table;

@Entity

@Table(name = "app_user")

public class User {

@Id

@GeneratedValue

private Integer id;

private String uname;

private String pword;

/*

* one User can have many phone numbers. CascadeType.ALL causes associated

* phone numbers to be delted when a User is deleted.

*/

@ManyToMany(cascade=CascadeType.ALL)

@JoinTable(

name="user_number",

joinColumns = { @JoinColumn( name="user_id") },

inverseJoinColumns = @JoinColumn( name="phone_id")

)

private Set phoneNumbers;

public User() {

// TODO Auto-generated constructor stub

}

public String toString() {

return "User [id=" + id + ", pword=" + pword + ", uname=" + uname + ", phoneNumbers]";

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getUname() {

return uname;

}

public void setUname(String uname) {

this.uname = uname;

}

public String getPword() {

return pword;

}

public void setPword(String pword) {

this.pword = pword;

}

public Set getPhoneNumbers() {

return phoneNumbers;

}

}