package dao;
import java.sql.*;import beans.ShopBean;
import Connect.Dbconnection;
public class ShopDAO {
private ShopBean shop; public ShopBean getShop() {
return shop;
} public void setShop(ShopBean shop) {
this.shop = shop;
}
public boolean checkshop(ShopBean s) throws SQLException{
boolean flag=false;
Connection conn=Dbconnection.getconnection();
String sql="select isshopman from users where userid='"+s.getUserid()+"'";
Statement st=null;
ResultSet rs=null;
try{
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
if(rs.getInt("isshopman")==0)
{
flag=true;
}
}
}catch(Exception e){
System.out.println("error");
}
finally
{
rs.close();
st.close();
conn.close();
}
return flag;
}
public boolean addshop(ShopBean s) throws SQLException{
boolean flag=false;
if(checkshop(s)){
Connection conn=Dbconnection.getconnection();
String sql = "insert into shop "+ "(shopname,resume,userid)"+ "values(?,?,?)";
PreparedStatement ps=null;
try{
ps=conn.prepareStatement(sql);
ps.setString(1,s.getShopname());
ps.setString(2, s.getResume());
ps.setInt(3, s.getUserid());
ps.executeUpdate(sql);
flag=true;
}catch(Exception e){
e.printStackTrace();;
}
finally
{
conn.close();
}
}
return flag;
}
public static void main(String []args) throws SQLException
{
ShopDAO dao=new ShopDAO();
ShopBean s=new ShopBean();
s.setResume("sss");
s.setShopname("wohao ");
s.setUserid(21);
if(dao.addshop(s)){
System.out.println("success");
}
}
}
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?)' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2548)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1605)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1524)
at dao.ShopDAO.addshop(ShopDAO.java:57)
at dao.ShopDAO.main(ShopDAO.java:76)高人来教我
import java.sql.*;import beans.ShopBean;
import Connect.Dbconnection;
public class ShopDAO {
private ShopBean shop; public ShopBean getShop() {
return shop;
} public void setShop(ShopBean shop) {
this.shop = shop;
}
public boolean checkshop(ShopBean s) throws SQLException{
boolean flag=false;
Connection conn=Dbconnection.getconnection();
String sql="select isshopman from users where userid='"+s.getUserid()+"'";
Statement st=null;
ResultSet rs=null;
try{
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
if(rs.getInt("isshopman")==0)
{
flag=true;
}
}
}catch(Exception e){
System.out.println("error");
}
finally
{
rs.close();
st.close();
conn.close();
}
return flag;
}
public boolean addshop(ShopBean s) throws SQLException{
boolean flag=false;
if(checkshop(s)){
Connection conn=Dbconnection.getconnection();
String sql = "insert into shop "+ "(shopname,resume,userid)"+ "values(?,?,?)";
PreparedStatement ps=null;
try{
ps=conn.prepareStatement(sql);
ps.setString(1,s.getShopname());
ps.setString(2, s.getResume());
ps.setInt(3, s.getUserid());
ps.executeUpdate(sql);
flag=true;
}catch(Exception e){
e.printStackTrace();;
}
finally
{
conn.close();
}
}
return flag;
}
public static void main(String []args) throws SQLException
{
ShopDAO dao=new ShopDAO();
ShopBean s=new ShopBean();
s.setResume("sss");
s.setShopname("wohao ");
s.setUserid(21);
if(dao.addshop(s)){
System.out.println("success");
}
}
}
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?)' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2548)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1605)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1524)
at dao.ShopDAO.addshop(ShopDAO.java:57)
at dao.ShopDAO.main(ShopDAO.java:76)高人来教我
at dao.ShopDAO.main(ShopDAO.java:76)
错误在这,
先把药执行的sql打印到后台,如何到数据库中执行下,看能用不。程序没问题。
这样试试看行不!
你这个shop表只插入这3个字段数据!其他字段的可以为NULL?主键呢?如果要自增长的话
insert into shop (id,shopname,resume,userid) values(null,?,?,?)
然后将问号替换成一个实际值在执行下,看是否能成功。