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());
}
}
}

No comments:

Post a Comment