67,513
社区成员
发帖
与我相关
我的任务
分享
public void addDatas(Item i,Data d) throws Exception {
Connection con = getConnection();
String sql = "insert into data(did,dprice,iid) values(?,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, d.getDid());
pstmt.setString(2, d.getDprice());
pstmt.setString(3, i.getIid());
pstmt.executeUpdate(sql);
System.out.println(d.getDid()+""+d.getDprice()+":"+i.getIid());
pstmt.close();
con.close();
}
pstmt.executeUpdate(sql);
你等于直接在数据库里执行insert into data(did,dprice,iid) values(?,?,?),是不能执行成功的,prepareStatement是继承自Statement。
pstmt.executeUpdate()即可。
package cn.zx.rental.domain;
public class Data {
private String did;
private String dprice;
public String getDid() {
return did;
}
public void setDid(String did) {
this.did = did;
}
public String getDprice() {
return dprice;
}
public void setDprice(String dprice) {
this.dprice = dprice;
}
public Data(String did, String dprice) {
super();
this.did = did;
this.dprice = dprice;
}
public Data() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Data [did=" + did + ", dprice=" + dprice + "]";
}
}
Item.java
package cn.zx.rental.domain;
public class Item {
private String iid;
private String iurl;
public String getIid() {
return iid;
}
public void setIid(String iid) {
this.iid = iid;
}
public String getIurl() {
return iurl;
}
public void setIurl(String iurl) {
this.iurl = iurl;
}
public Item(String iid, String iurl) {
super();
this.iid = iid;
this.iurl = iurl;
}
public Item() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Item [iid=" + iid + ", iurl=" + iurl + "]";
}
}
Rule.java
package cn.zx.rental.domain;
/**
* 规则类
*/
public class Rule
{
/**
* 链接
*/
private String url;
/**
* 参数集合
*/
private String params;
/**
* 参数对应的值
*/
private String values;
/**
* 对返回的HTML,第一次过滤所用的标签,请先设置type
*/
private String resultTagName;
/**
* CLASS / ID / SELECTION
* 设置resultTagName的类型,默认为ID
*/
private int type = ID ;
/**
*GET / POST
* 请求的类型,默认GET
*/
private int requestMoethod = GET ;
private String tagType;
public final static int GET = 0 ;
public final static int POST = 1 ;
public final static int CLASS = 0;
public final static int ID = 1;
public final static int SELECTION = 2;
public Rule()
{
}
public Rule(String url, String params, String values,
String resultTagName, int type, int requestMoethod, String tagType)
{
super();
this.url = url;
this.params = params;
this.values = values;
this.resultTagName = resultTagName;
this.type = type;
this.requestMoethod = requestMoethod;
this.tagType = tagType;
}
public String getUrl()
{
return url;
}
public void setUrl(String url)
{
this.url = url;
}
public String getParams()
{
return params;
}
public void setParams(String params)
{
this.params = params;
}
public String getValues()
{
return values;
}
public void setValues(String values)
{
this.values = values;
}
public String getResultTagName()
{
return resultTagName;
}
public void setResultTagName(String resultTagName)
{
this.resultTagName = resultTagName;
}
public int getType()
{
return type;
}
public void setType(int type)
{
this.type = type;
}
public int getRequestMoethod()
{
return requestMoethod;
}
public void setRequestMoethod(int requestMoethod)
{
this.requestMoethod = requestMoethod;
}
public String getTagType() {
return tagType;
}
public void setTagType(String tagType) {
this.tagType = tagType;
}
}
package cn.zx.rental.domain;
public class RuleException extends RuntimeException
{
private static final long serialVersionUID = 1L;
public RuleException()
{
super();
// TODO Auto-generated constructor stub
}
public RuleException(String message, Throwable cause)
{
super(message, cause);
// TODO Auto-generated constructor stub
}
public RuleException(String message)
{
super(message);
// TODO Auto-generated constructor stub
}
public RuleException(Throwable cause)
{
super(cause);
// TODO Auto-generated constructor stub
}
}
package cn.zx.rental.domain;
public class TextTypeData {
private String text;
private String id;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getId() {
return id;
}
public void setId(String string) {
this.id = string;
}
@Override
public String toString() {
return "TextTypeData [text=" + text + ", id=" + id + "]";
}
}
package cn.zx.rental.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import cn.zx.rental.domain.Data;
import cn.zx.rental.domain.Item;
public class RentalDao {
public static Connection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/rental";
return DriverManager.getConnection(url, "root", "zhangxin");
}
public void addItems(Item i) throws Exception {
Connection con = getConnection();
String sql = "insert into item(iid,iurl) values(?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, i.getIid());
pstmt.setString(2, i.getIurl());
pstmt.executeUpdate(sql);
System.out.println(i.getIid()+":"+i.getIurl()+"additems");
pstmt.close();
con.close();
}
public void addDatas(Item i,Data d) throws Exception {
Connection con = getConnection();
String sql = "insert into data(did,dprice,iid) values(?,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, d.getDid());
pstmt.setString(2, d.getDprice());
pstmt.setString(3, i.getIid());
pstmt.executeUpdate(sql);
System.out.println(d.getDid()+""+d.getDprice()+":"+i.getIid());
pstmt.close();
con.close();
}
/*public void addItems(Item i){
String sql = "insert into item(iid,iurl) values(?,?)";
Object[] params = {
i.getIid(),i.getIurl()
};
try {
qr.update(sql,params);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void addDatas(Item i,TextTypeData d){
String sql = "insert into data(did,dprice,iid) values(?,?,?)";
Object[] params = {
d.getId(),d.getText(),i.getIid()
};
try {
qr.update(sql,params);
} catch (Exception e) {
throw new RuntimeException(e);
}
}*/
}