package com.acetech.ramon.web;
import java.sql.*;
public class User { public User() {
}
public static void main(String[] args) throws Exception{ //User user = User.findById("2");
//System.out.println(user.getUid());
//System.out.println(user.getName());
//System.out.println(user.getPassword());
//boolean falg = User.deleteById("2");
//System.out.println(falg);
User.insertUid("71","hanlintongxue","13079800879");
// System.out.println(user2.getUid());
// System.out.println(user2.getName());
// System.out.println(user2.getPassword());
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUid() {
return uid;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
/******************************************************************************/
public static User findById(String uid) throws Exception{
// int uid = Integer.parseInt(suid);
Connection conn = DBManager.getConnection();
PreparedStatement ps = conn.prepareStatement("SELECT * from user WHERE uid=?");
ps.setString(1,uid);
ResultSet rs = ps.executeQuery();
User user = new User();
if (rs.next()){
user.setUid(rs.getString(1));
user.setName(rs.getString(2));
user.setPassword(rs.getString(3));
}
rs.close();
ps.close();
conn.close();
return user;
}
public static boolean deleteById(String uid) throws Exception {
Connection conn = DBManager.getConnection();
conn.setAutoCommit(false);
PreparedStatement ps = conn.prepareStatement("DELETE from user where uid=?");
ps.setString(1,uid);
int i = ps.executeUpdate();
boolean flag=true;
if (i==0){
flag = true;
}
conn.commit();
return flag; }
public static void updateById(String uid,String name, String password) throws Exception{
Connection conn = DBManager.getConnection();
PreparedStatement ps = conn.prepareStatement("UPDATE user SET name=?,password=? WHERE uid=?");
ps.setString(1,name);
ps.setString(2,password);
ps.setString(3,uid);
int i = ps.executeUpdate();
ps.close();
conn.close();
}
public static User insertUid(String uid,String name,String password) throws Exception {
Connection conn = DBManager.getConnection();
PreparedStatement ps = conn.prepareStatement("SELECT uid from user order by uid desc");
ResultSet rs = ps.executeQuery();
User user = new User();
if (rs.next()){
String ra=rs.getString(1);
//
// else ra=0; int temp=Integer.parseInt(ra);
temp = temp + 1;
String ra1=Integer.toString(temp);
rs.close();
ps.close();
}
PreparedStatement ps1 = conn.prepareStatement("INSERT INTO user VALUES(?,?,?)");
ps1.setString(1,uid);
ps1.setString(2,name);
ps1.setString(3,password);
int i = ps1.executeUpdate();
ps1.close();
conn.close();
return user;
}/******************************************************************************/ private String uid;
private String name;
private String password;
import java.sql.*;
public class User { public User() {
}
public static void main(String[] args) throws Exception{ //User user = User.findById("2");
//System.out.println(user.getUid());
//System.out.println(user.getName());
//System.out.println(user.getPassword());
//boolean falg = User.deleteById("2");
//System.out.println(falg);
User.insertUid("71","hanlintongxue","13079800879");
// System.out.println(user2.getUid());
// System.out.println(user2.getName());
// System.out.println(user2.getPassword());
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUid() {
return uid;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
/******************************************************************************/
public static User findById(String uid) throws Exception{
// int uid = Integer.parseInt(suid);
Connection conn = DBManager.getConnection();
PreparedStatement ps = conn.prepareStatement("SELECT * from user WHERE uid=?");
ps.setString(1,uid);
ResultSet rs = ps.executeQuery();
User user = new User();
if (rs.next()){
user.setUid(rs.getString(1));
user.setName(rs.getString(2));
user.setPassword(rs.getString(3));
}
rs.close();
ps.close();
conn.close();
return user;
}
public static boolean deleteById(String uid) throws Exception {
Connection conn = DBManager.getConnection();
conn.setAutoCommit(false);
PreparedStatement ps = conn.prepareStatement("DELETE from user where uid=?");
ps.setString(1,uid);
int i = ps.executeUpdate();
boolean flag=true;
if (i==0){
flag = true;
}
conn.commit();
return flag; }
public static void updateById(String uid,String name, String password) throws Exception{
Connection conn = DBManager.getConnection();
PreparedStatement ps = conn.prepareStatement("UPDATE user SET name=?,password=? WHERE uid=?");
ps.setString(1,name);
ps.setString(2,password);
ps.setString(3,uid);
int i = ps.executeUpdate();
ps.close();
conn.close();
}
public static User insertUid(String uid,String name,String password) throws Exception {
Connection conn = DBManager.getConnection();
PreparedStatement ps = conn.prepareStatement("SELECT uid from user order by uid desc");
ResultSet rs = ps.executeQuery();
User user = new User();
if (rs.next()){
String ra=rs.getString(1);
//
// else ra=0; int temp=Integer.parseInt(ra);
temp = temp + 1;
String ra1=Integer.toString(temp);
rs.close();
ps.close();
}
PreparedStatement ps1 = conn.prepareStatement("INSERT INTO user VALUES(?,?,?)");
ps1.setString(1,uid);
ps1.setString(2,name);
ps1.setString(3,password);
int i = ps1.executeUpdate();
ps1.close();
conn.close();
return user;
}/******************************************************************************/ private String uid;
private String name;
private String password;
这样吧,我给你一个例子,对你有帮助:
//CustomerBean.java
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;public class CustomerBean implements EntityBean { private String customerId;
private String salesRepId;
private String name;
private Connection con;
private String dbName = "java:comp/env/jdbc/SalesDB";
private EntityContext context;
public String getSalesRepId() { return salesRepId;
}
public String getName() { System.out.println("entering getName()");
return name;
} public void setSalesRepId(String salesRepId) { this.salesRepId = salesRepId;
}
public void setName(String name) { this.name = name;
} public String ejbCreate(String customerId, String salesRepId,
String name) throws CreateException {
System.out.println("in ejbCreate");
try {
insertCustomer(customerId, salesRepId, name);
} catch (Exception ex) {
throw new EJBException("ejbCreate: " +
ex.getMessage());
} this.customerId = customerId;
this.salesRepId = salesRepId;
this.name = name; System.out.println("about to leave ejbCreate");
return customerId;
}
public String ejbFindByPrimaryKey(String primaryKey)
throws FinderException { boolean result; try {
result = selectByPrimaryKey(primaryKey);
} catch (Exception ex) {
throw new EJBException("ejbFindByPrimaryKey: " +
ex.getMessage());
} if (result) {
return primaryKey;
}
else {
throw new ObjectNotFoundException
("Row for id " + primaryKey + " not found.");
}
} public Collection ejbFindBySalesRep(String salesRepId)
throws FinderException { Collection result; try {
result = selectBySalesRep(salesRepId);
} catch (Exception ex) {
throw new EJBException("ejbFindBySalesRep: " +
ex.getMessage());
}
return result;
} public void ejbRemove() { try {
deleteCustomer(customerId);
} catch (Exception ex) {
throw new EJBException("ejbRemove: " +
ex.getMessage());
}
} public void setEntityContext(EntityContext context) { this.context = context;
try {
makeConnection();
} catch (Exception ex) {
throw new EJBException("Unable to connect to database. " +
ex.getMessage());
}
} public void unsetEntityContext() { try {
con.close();
} catch (SQLException ex) {
throw new EJBException("unsetEntityContext: " + ex.getMessage());
}
} public void ejbActivate() { customerId = (String)context.getPrimaryKey();
} public void ejbPassivate() { customerId = null;
}
public void ejbLoad() { System.out.println("in ejbLoad");
try {
loadCustomer();
} catch (Exception ex) {
throw new EJBException("ejbLoad: " +
ex.getMessage());
}
System.out.println("leaving ejbLoad");
} public void ejbStore() { System.out.println("in ejbStore");
try {
storeCustomer();
} catch (Exception ex) {
throw new EJBException("ejbStore: " +
ex.getMessage());
}
System.out.println("leaving ejbStore");
} public void ejbPostCreate(String customerId, String salesRepId,
String name) { }/*********************** Database Routines *************************/ private void makeConnection() throws NamingException, SQLException { InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);
con = ds.getConnection();
} private void insertCustomer (String customerId, String salesRepId,
String name) throws SQLException { String insertStatement =
"insert into customer values ( ? , ? , ? )";
PreparedStatement prepStmt =
con.prepareStatement(insertStatement); prepStmt.setString(1, customerId);
prepStmt.setString(2, salesRepId);
prepStmt.setString(3, name); prepStmt.executeUpdate();
prepStmt.close();
} private boolean selectByPrimaryKey(String primaryKey)
throws SQLException { String selectStatement =
"select customerid " +
"from customer where customerid = ? ";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement);
prepStmt.setString(1, primaryKey); ResultSet rs = prepStmt.executeQuery();
boolean result = rs.next();
prepStmt.close();
return result;
} private Collection selectBySalesRep(String salesRepId)
throws SQLException { String selectStatement =
"select customerid " +
"from customer where salesrepid = ? ";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement); prepStmt.setString(1, salesRepId);
ResultSet rs = prepStmt.executeQuery();
ArrayList a = new ArrayList(); while (rs.next()) {
String id = rs.getString(1);
a.add(id);
} prepStmt.close();
return a;
} private void deleteCustomer(String customerId) throws SQLException { String deleteStatement =
"delete from customer " +
"where customerid = ?";
PreparedStatement prepStmt =
con.prepareStatement(deleteStatement); prepStmt.setString(1, customerId);
prepStmt.executeUpdate();
prepStmt.close();
} private void loadCustomer() throws SQLException { String selectStatement =
"select customerid, salesRepid, name " +
"from customer where customerid = ? ";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement); prepStmt.setString(1, customerId); ResultSet rs = prepStmt.executeQuery(); if (rs.next()) {
customerId = rs.getString(1);
salesRepId = rs.getString(2);
name = rs.getString(3);
prepStmt.close();
}
else {
prepStmt.close();
throw new NoSuchEntityException("Row for customerId " + customerId +
" not found in database.");
}
} private void storeCustomer() throws SQLException { System.out.println("entering storeCustomer");
String updateStatement =
"update customer " +
"set salesRepid = ? , name = ? " +
"where customerid = ?";
PreparedStatement prepStmt =
con.prepareStatement(updateStatement); prepStmt.setString(1, salesRepId);
prepStmt.setString(2, name);
prepStmt.setString(3, customerId);
int rowCount = prepStmt.executeUpdate();
prepStmt.close(); if (rowCount == 0) {
throw new EJBException("Storing row for customerId " +
customerId + " failed.");
}
System.out.println("leaving storeCustomer");
}} // CustomerBean
还有一件事没有和你说呢,我写的好象是无状态对话bean
为什么还要无状态对话bean