Java 使用jsp从数据库中删除数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24429224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete data from database using jsp
提问by Shakir Ali
I want to delete a particular record from the database.
我想从数据库中删除特定记录。
My code is given below:
我的代码如下:
<%
String value = request.getParameter("Meter_No");
int v=Integer.parseInt(value);
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDirver");
Connection conn = DriverManager.getConnection ("jdbc:sqlserver://localhost:1433;databaseName=myDatabase;
user=shakir;password=shakir123");
Statement st = conn.createStatement();
ResultSet rs =st.executeQuery("DELETE * FROM qesco_table
WHERE Meter_No ="+v+"");
rs.close();
conn.close();
}catch(Exception e){
System.out.print(e.getMessage());
}
%>
But it is not deleting the data from database.
但它不会从数据库中删除数据。
Can anyone guide me that where is the problem with the code?
任何人都可以指导我代码的问题在哪里?
采纳答案by gprathour
Every thing in your code is fine.
你的代码中的每一件事都很好。
But You need to run your query using st.executeUpdate()
.
但是您需要使用st.executeUpdate()
.
So change the following line
所以更改以下行
ResultSet rs =st.executeQuery("DELETE * FROM qesco_table WHERE Meter_No ="+v+"");
to
到
st.executeUpdate("DELETE * FROM qesco_table WHERE Meter_No ="+v);
PLUS
加
You don't need to have ResultSet in this program as your query is not going to return you any data to store.
You don't need to have empty "" (double quotes) at the end of your query.
You should close connection and others in
finally
block rather than try block itself.
您不需要在此程序中使用 ResultSet,因为您的查询不会返回任何要存储的数据。
您不需要在查询末尾有空的 ""(双引号)。
您应该在
finally
块中关闭连接和其他人,而不是尝试块本身。
And better if you try to use PreparedStatementsto write dynamic queries. So it will become something like,
如果您尝试使用PreparedStatements编写动态查询,效果会更好。所以它会变成这样,
<%
String value = request.getParameter("Meter_No");
int v=Integer.parseInt(value);
Connection conn = null;
PreparedStatement pst = null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDirver");
conn = DriverManager.getConnection ("jdbc:sqlserver://localhost:1433;databaseName=myDatabase;user=shakir;password=shakir123");
pst = conn.prepareStatement("delete from qesco_table where Meter_No = ?");
pst.setInt(1,v);
pst.executeUpdate();
}catch(Exception e){
System.out.print(e.getMessage());
}finally{
pst.close();
conn.close();
}
%>
回答by mahesh
To delete a record from data base using JDBC you need to use executeUpdate("your query")
method.The executeQuery()
query is used when you want to retrieve data from data base.
要使用 JDBC 从数据库中删除记录,您需要使用executeUpdate("your query")
方法。executeQuery()
当您想从数据库中检索数据时使用查询。
Then query should be
那么查询应该是
DELETE FROM qesco_table
WHERE Meter_No ="+v+"
It is not Delete * from table
and correct is Delete from table
不 Delete * from table
正确的是Delete from table
Change to
改成
st.executeUpdate("DELETE FROM qesco_table WHERE Meter_No ="+v);
回答by SparkOn
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDirver");
Connection conn = DriverManager.getConnection ("jdbc:sqlserver://localhost:1433;databaseName=myDatabase;user=shakir;password=shakir123");
Statement st = conn.createStatement();
ResultSet rs =st.executeUpdate("DELETE * FROM qesco_table WHERE Meter_No ="+v);
}
catch(SQLException e){
System.out.print(e.getMessage());
}
finally
{
rs.close();
conn.close();
}
回答by Braj
I suggest to avoid Scriplet instead use JSP Standard Tag Libraryand Expression language that is easy to user and less error prone.
我建议避免使用 Scriplet,而是使用易于用户且不易出错的JSP 标准标记库和表达式语言。
One more thing you should move this database code in a servlet and call it from JSP. If you are still interested to do it in JSP then you should use SQL Tag Librarythat is designed for database access from JSP.
还有一件事,您应该将这个数据库代码移动到一个 servlet 中并从 JSP 中调用它。如果您仍然有兴趣在 JSP 中执行此操作,那么您应该使用专为从 JSP 访问数据库而设计的SQL 标记库。
Sample code:
示例代码:
<sql:setDataSource var="dataSource"
driver="com.microsoft.sqlserver.jdbc.SQLServerDirver"
url="jdbc:sqlserver://localhost:1433;databaseName=myDatabase"
user="shakir" password="shakir123" />
<sql:update dataSource="${dataSource}"
sql="DELETE FROM qesco_table WHERE Meter_No =?">
<sql:param value="${param.Meter_No }" />
</sql:update>
回答by wrecklez
try this bro!! it will work for sure 101%
试试这个兄弟!!它肯定会工作 101%
<%
String value = request.getParameter("Meter_No");
int v=Integer.parseInt(value);
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDirver");
Connection conn = DriverManager.getConnection ("jdbc:sqlserver://localhost:1433;databaseName=myDatabase;user=shakir;password=shakir123");
Statement st = conn.createStatement();
String sql = "DELETE FROM qesco_table WHERE Meter_No= '"+v+"'";
st.executeUpdate(sql);
}
catch(Exception e){
System.out.print(e.getMessage());
}
%>
回答by Raju Irale
Class.forName("com.mysql.cj.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/advjava", "root", "root");
Statement stat=c.createStatement();
int del=stat.executeUpdate("Delete from Cart where U_Id="+uid);