【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了CustomerDM.java提供大家参考,希望对大家有所帮助!
package com.abc.store.dm;
import com.abc.store.pd.*;
import com.mortbay.Base.*;
import java.sql.*;
import java.util.*;
public class CustomerDM
{
public static boolean loadByNamePassword(Customer customer)
{
String loginName = customer.getLoginName();
String password = customer.getPassword();
String sqlStr = "select * from customer where loginname='"
+ loginName + "' and password='" + password + "'";
return load(customer, sqlStr);
}
public static boolean load(Customer customer)
{
int id = customer.getId();
String sqlStr = "select * from customer where id=" + id;
return load(customer, sqlStr);
}
public static boolean load(Customer customer, String sqlStr)
{
if(Code.verbose(30)) Code.debug("sqlStr=" +
sqlStr);
boolean flag = false;
Statement stmt = null;
ResultSet rs = null;
try
{
Connection conn = DMHelper.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlStr);
if(rs==null) return false;
while (rs.next())
{
customer.setName(rs.getString("name"));
customer.setPassword(rs.getString("password"));
customer.setStreet(rs.getString("street"));
customer.setCityState(rs.getString("cityState"));
customer.setPhone(rs.getString("phone"));
flag = true;
}
}
catch(Exception ex)
{
System.out.println("CustomerDM.load(): " + ex);
}
return flag;
}
public static Customer add(Customer customer)
{
String name = customer.getName();
String pass
word = customer.getPassword();
String street = customer.getStreet();
String cityState = customer.getCityState();
String phone = customer.getPhone();
String email = customer.getEmail();
String sqlStr = "insert into customer " +
"(name, password, street, cityState, phone, email) values('"
+ name + "','"+password+"','"+street+"','"+cityState+"','"+phone+"','"+email+"')" ;
if(Code.verbose(30)) Code.debug("sqlStr=" + sqlStr);
Statement stmt = null;
ResultSet rs = null;
try
{
Connection conn = DMHelper.getConnection();
stmt = conn.createStatement();
stmt.executeUpdate(sqlStr);
stmt = conn.createStatement();
sqlStr = "select id from customer where name='"+name+"' and password='" + password + "'";
rs = stmt.executeQuery(sqlStr);
if(rs==null) return null;
while (rs.next())
{
int id = rs.getInt("id");
customer.setId(id);
}
}
catch(Exception ex)
{
System.err.println("CustomerDM.add(): " + ex);
}
return customer;
}
public static void main(String arg[])
{
int id = 1;
Customer customer = new Customer(id);
customer.load();
System.out.println(customer.getName());
System.out.println(customer.getPassword());
}
}