连数据库都不知道,怎么让我们写sql语句;连用的什么编程语言都不知道,怎么让我们写后台代码,连html结构都不知道,怎么让我们写前台代码。如果这些都知道,你存在的价值也就不存在了,你问这个问题的意义也就不存在了,我们直接帮你写完算了。
您的SQL语法错误,检查手册,对应于你的MySQL语法的正确使用服务器版本位于第1行近'其中ID
=
'71201'
语法错误,你看看ID是不是主键,如果是主键的话,那就是主外键关联所以删除失败!
这个要看看你用java添加数据时,执行sql语句后是否提交,或者是java执行后是否断开连接。正常情况下代码向数据库添加的数据是可以删除的。
你38行那里是肿么回事?37行刚刚执行了insert的SQL语句,38行又在执行,这里不正确吧。是不是应该写rs=pstexecuteQuery();而不是pst1,因为pst1里面传的是个insert语句啊。
先把要删除的元组封装为一个对象例如user
package a;
public class User {
public int id;
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
thisage = age;
}
public String name;
public int age;
}
在 *** 作数据库,我用的MySQL
package a;
import javasql;
public class DB {
public static Connection getConn() throws Exception{
ClassforName("commysqljdbcDriver");
String url="jdbc:mysql://localhost:3306/test";
Connection connection=DriverManagergetConnection(url, "root", "123");
return connection;
}
public static void main(String[] args) throws Exception {
Connection conn=getConn();
Statement s=conncreateStatement(ResultSetTYPE_SCROLL_INSENSITIVE, ResultSetTYPE_FORWARD_ONLY);
String sql="select from user";
ResultSet rs=sexecuteQuery(sql);
rsfirst();
User user=new User();
usersetId(rsgetInt("id"));
usersetAge(rsgetInt("age"));
usersetName(rsgetString("name"));
String sql2="insert into user2 values(,,);";
PreparedStatement ps=connprepareStatement(sql2);
pssetInt(1, usergetId());
pssetString(2, usergetName());
pssetInt(3, usergetAge());
try {
psexecuteUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
数据库插入默认最后一条记录
其实,也就那几步而已:
别忘了,加载jar文件
加载驱动
获得Connection
获得PreparedStatement /PreparedStatement
执行sql语句
处理结果集ResultSet
关闭连接
mport javasqlConnection;
import javasqlPreparedStatement;
import javasqlResultSet;
import javautilArrayList;
import javautilList;
import EntityJIAJU;
public class JiaJu {
public JIAJU selectExe(int shouhinId) {
JIAJU jia = new JIAJU();
try {
Connection con = ConnectionManagergetConnection();
String sql = "select from jiaju where shouhinId=";
PreparedStatement ps = conprepareStatement(sql);
pssetInt(1, shouhinId);
ResultSet rs = psexecuteQuery();
if (rs != null) {
while (rsnext()) {
jiasetShouhinId(rsgetInt("shouhinId"));
jiasetShouhinName(rsgetString("shouhinName"));
jiasetShouhinColor(rsgetString("shouhinColor"));
jiasetShouhinPrice(rsgetInt("shouhinPrice"));
jiasetShouhinPai(rsgetString("shouhinPai"));
jiasetShouhinShi(rsgetString("shouhinShi"));
// listadd(jia);
}
}
} catch (Exception e) {
eprintStackTrace();
}
return jia;
}
public void insertJia(JIAJU jia) {
try {
Connection con = ConnectionManagergetConnection();
String sql = "insert into jiaju values(,,,,)";
PreparedStatement ps = conprepareStatement(sql);
pssetString(1, jiagetShouhinName());
pssetString(2, jiagetShouhinColor());
pssetInt(3, jiagetShouhinPrice());
pssetString(4, jiagetShouhinPai());
pssetString(5, jiagetShouhinShi());
psexecuteUpdate();
} catch (Exception e) {
eprintStackTrace();
}
}
public List selectJia() {
List list = new ArrayList();
try {
Connection con = ConnectionManagergetConnection();
String sql = "select from jiaju ";
PreparedStatement ps = conprepareStatement(sql);
ResultSet rs = psexecuteQuery();
if (rs != null) {
while (rsnext()) {
JIAJU jia = new JIAJU();
jiasetShouhinId(rsgetInt("shouhinId"));
jiasetShouhinName(rsgetString("shouhinName"));
jiasetShouhinColor(rsgetString("shouhinColor"));
jiasetShouhinPrice(rsgetInt("shouhinPrice"));
jiasetShouhinPai(rsgetString("shouhinPai"));
jiasetShouhinShi(rsgetString("shouhinShi"));
listadd(jia);
}
}
} catch (Exception e) {
eprintStackTrace();
}
return list;
}
public JIAJU selectbuy(int shouhinId) {
JIAJU jia = new JIAJU();
try {
Connection con = ConnectionManagergetConnection();
String sql = "select from jiaju where shouhinId=";
PreparedStatement ps = conprepareStatement(sql);
pssetInt(1, shouhinId);
ResultSet rs = psexecuteQuery();
if (rs != null) {
while (rsnext()) {
jiasetShouhinId(rsgetInt("shouhinId"));
jiasetShouhinName(rsgetString("shouhinName"));
jiasetShouhinColor(rsgetString("shouhinColor"));
jiasetShouhinPrice(rsgetInt("shouhinPrice"));
jiasetShouhinPai(rsgetString("shouhinPai"));
jiasetShouhinShi(rsgetString("shouhinShi"));
}
}
} catch (Exception e) {
eprintStackTrace();
}
return jia;
}
public void updateLou(JIAJU jia){
try{
Connection con = ConnectionManagergetConnection();
String sql = "update jiaju set shouhinPrice= where shouhinId=";
PreparedStatement ps = conprepareStatement(sql);
pssetInt(1,jiagetShouhinPrice());
pssetInt(2, jiagetShouhinId());
psexecuteUpdate();
}catch(Exception e){
eprintStackTrace();
}
}
public void deleteLou(JIAJU jia){
try{
Connection con = ConnectionManagergetConnection();
String sql = "delete from jiaju where shouhinId=";
PreparedStatement ps = conprepareStatement(sql);
pssetInt(1, jiagetShouhinId());
psexecuteUpdate();
}catch(Exception e){
eprintStackTrace();
}
}
}
方法为:
在数据库中增加一个标志字段
在页面中如果满足条件那么让空上标志字段为真
那么就可以在页面显示上即不显示该标志字段为真的内容,否则就显示
说明:具体实现要以具体业务逻辑为准,以上为解决思想,理解便可解决该问题。
以上就是关于java 从数据库中删除用户全部的内容,包括:java 从数据库中删除用户、JAVA中 删除Mysql数据库中数据应该怎么写、在JAVA 中像数据库中插入的数据怎么在数据库中删除等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)