你好,我用的是sqlserver2005数据库代码如下:import javasql;
public class Demo {
public static void main(String agrs[]) {
Connection con = null;
PreparedStatement pstmt = null;
String sql = "delete from user where username=";
try {
ClassforName("commicrosoftsqlserverjdbcSQLServerDriver"); //设置数据库连接的驱动
con = DriverManagergetConnection("jdbc:sqlserver://localhost:1433;databaseName=数据库"); //设置数据库连接的 URL,用户名,密码
pstmt = conprepareStatement(sql);
pstmtsetString(1, "aaa"); // 设置SQL语句中username的值
int count = pstmtexecuteUpdate();
if (count > 0) {
Systemoutprintln(" *** 作成功");
} else {
Systemoutprintln(" *** 作失败");
}
} catch (Exception e) {
eprintStackTrace();
}
}
}
这个要看看你用java添加数据时,执行sql语句后是否提交,或者是java执行后是否断开连接。正常情况下代码向数据库添加的数据是可以删除的。
你38行那里是肿么回事?37行刚刚执行了insert的SQL语句,38行又在执行,这里不正确吧。是不是应该写rs=pstexecuteQuery();而不是pst1,因为pst1里面传的是个insert语句啊。
其实,也就那几步而已:
别忘了,加载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();
}
}
}
可以使用HibernateTemplate执行Hql语句 *** 作。
String userId = "somebody";String hql ="delete from ATable as a where auserId = "+ userId +"";
thisgetHibernateTemplate()bulkUpdate(queryString);
你说的不是很清楚,如果是指定的一条记录的话,只要把username的值加上去就行
具体步骤如下:
1连接数据库。
2用一个preparestatement预编译下。
3然后执行把username的值赋给sql语句中的问号
4执行sql语句
代码如下:
Connection connection = DriverManager,getConnection();
String sql = "delete from 表明 where username = ";
PreparedStatement preparedStatement = connectionprepareStatement(sql);
preparedStatementsetString(1,"username");
preparedStatementexcute();
报错了就catch下,之后把在finally语句中依次把preparedconnection,connection关闭即可
有问题可追问
以上就是关于关于在java中删除数据库的一条记录全部的内容,包括:关于在java中删除数据库的一条记录、在JAVA 中像数据库中插入的数据怎么在数据库中删除、java 如何连接数据库,如何完成数据库记录添加,删除,修改以及查询。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)