你好,我用的是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();
}
}
}
1增加\x0d\String s1="insert into tableNames (id,name,password) values(myseqnextval,,);"\x0d\ClassforName(driver);\x0d\Connection conn = DriverManagergetConnection(url,dbUser,dbPwd);\x0d\PreparedStatement prepStmt = connprepareStatement(s1);\x0d\prepStmtsetString(1,name);\x0d\prepStmtsetString(2,password);\x0d\ResultSet rs=stmtexecuteUpdate();\x0d\2、删除\x0d\String s2="delete from tbNames where name=";\x0d\ClassforName(driver);\x0d\Connection conn = DriverManagergetConnection(url,dbUser,dbPwd);\x0d\PreparedStatement prepStmt = connprepareStatement(s2);\x0d\prepStmtsetString(1,name);\x0d\ResultSet rs=stmtexecuteUpdate();\x0d\3、修改\x0d\String s3=“update tbNames set name= where id=”;\x0d\ClassforName(driver);\x0d\Connection conn = DriverManagergetConnection(url,dbUser,dbPwd);\x0d\PreparedStatement prepStmt = connprepareStatement(s3);\x0d\prepStmtsetString(1,name);\x0d\prepStmtsetString(2,id);\x0d\ResultSet rs=stmtexecuteUpdate();\x0d\4、查询\x0d\String s4="select id,name,password from tbNames";\x0d\ClassforName(driver);\x0d\Connection conn = DriverManagergetConnection(url,dbUser,dbPwd);\x0d\Statement stmt=conncreateStatement();\x0d\ResultSet rs = stmtexecuteQuery(s4);\x0d\while(rsnext){\x0d\int id=rsgetInt(1);\x0d\String name = rsgetString(2);\x0d\String pwd=rsgetString(3);\x0d\Systemoutprintln(id+name+pwd); } \x0d\\x0d\以上四步必须都得关闭连接;!!!\x0d\rsclose();\x0d\stmtclose();\x0d\connclose();
以上就是关于关于在java中删除数据库的一条记录全部的内容,包括:关于在java中删除数据库的一条记录、怎样编写一个java程序能通过jdbc访问数据库实现对数据库的插入、删除、修改和查询、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)