Example of a JDBC implementation batch deletion

Source: Internet
Author: User

Database: SQLServer2005
Server: Tomcat6.0
Development tools: MyEclipse6.0
Adopt Frame: Struts1.2

Paste the complete code:

To create a SQL script:
CREATE TABLE [dbo]. [Batch] (
[ID] [int] IDENTITY (1,1) not NULL,
[Name] [varchar] (m) NULL,
[Password] [varchar] (m) NULL,
CONSTRAINT [Pk_batch] PRIMARY KEY CLUSTERED
(
[ID] ASC
With (Pad_index = off, Statistics_norecompute = off, Ignore_dup_key = off, Allow_row_locks = on, Allow_page_locks = O N) on [PRIMARY]
) on [PRIMARY]

Go

To connect to a database class:
public class DbConnection {
private static dbconnection DBC = null;

public static Connection Getdbconnection () throws Exception {
if (DBC = = null) {
DBC = new DbConnection ();
}
return Dbc._getconnection ();
}

Public Connection _getconnection () throws Exception {
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
Return Drivermanager.getconnection (
"Jdbc:sqlserver://localhost:1433;databasename=dbtest", "sa",
"sqlserver2005");
}
}

Adopt DAO Factory Mode:

Po class:
public class Batch {
private int id;
private String name;
private String password;

public int getId () {
return ID;
}

public void setId (int id) {
This.id = ID;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public String GetPassword () {
return password;
}

public void SetPassword (String password) {
This.password = password;
}
}

DAO Interface:

Public interface Batchdao {
Public list<batch> Queryall ();

public void Deleteselect (int[] str);
}

Implementing Interface classes:

public class Batchdaoimpl implements Batchdao {
Connection con = null;
PreparedStatement PST = NULL;
ResultSet rst = null;

public void Deleteselect (int[] str) {
String sql = "Delete from batch where ID in (";
if (str.length==1) {
sql=sql+str[0]+ ")";} else{
for (int i=0;i<str.length;i++) {
if (i==0) {
SQL=SQL+STR[0];
}
else if (i==str.length-1) {
Sql=sql+ "," +str[i]+ ")";
}else{
Sql=sql+ "," +str[i];
}
}
}
try {
con = dbconnection.getdbconnection ();
PST = con.preparestatement (SQL);
Pst.execute ();
catch (Exception e) {
E.printstacktrace ();
finally {
try {
if (PST!= null)
Pst.close ();
if (Con!= null)
Con.close ();
catch (Exception e) {
E.printstacktrace ();
}
}
}

Public list<batch> Queryall () {
list<batch> list = new arraylist<batch> ();
String sql = "SELECT * from Batch";
try {
con = dbconnection.getdbconnection ();
PST = con.preparestatement (SQL);
rst = Pst.executequery ();
while (Rst.next ()) {
Batch B = new Batch ();
B.setid (Rst.getint ("id"));
B.setname (rst.getstring ("name"));
B.setpassword (rst.getstring ("password"));
List.add (b);
}
catch (Exception e) {
E.printstacktrace ();
finally {
try {
if (RST!= null)
Rst.close ();
if (PST!= null)
Pst.close ();
if (Con!= null)
Con.close ();
catch (SQLException e) {
E.printstacktrace ();
}
}
return list;
}
}

Factory class:

public class Daofactory {
public static Batchdao Getbatchdaoimplinstance () {
return new Batchdaoimpl ();
}
}

Action: Inheriting Dispatchaction

public class Batchaction extends Dispatchaction {
/*
* Generated Methods
*/

/**
* Method Execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return Actionforward
*/
Public Actionforward Queryall (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response) {
list<batch> list = Daofactory.getbatchdaoimplinstance (). Queryall ();
Request.setattribute ("list", list);
Return Mapping.findforward ("display");
}

 public Actionforward Deleteselect (actionmapping mapping, actionform form,
    HttpServletRequest request, HttpServletResponse response) {
  string[] id = request.getparametervalues (" Name ");
  int[] i = new Int[id.length];
  for (int j = 0; J < Id.length + +) {
   i[j] = Integer.parseint (id[j));
&NBSP;&NBSP}
  daofactory.getbatchdaoimplinstance (). Deleteselect (i);
  return Mapping.findforward ("delete");
 }
}
Struts-config.xml
<?xml version= "1.0" encoding= "UTF-8"
<! DOCTYPE struts-config Public "-//apache Software foundation//dtd struts Configuration 1.2//en" "http:// Struts.apache.org/dtds/struts-config_1_2.dtd ">

<struts-config>
<data-sources/>
<form-beans/>
<global-exceptions/>
<global-forwards/>
<action-mappings >
<action
input= "/index.jsp"
parameter= "Batch"
Path= "/batch"
Type= "Com.batch.delete.struts.action.BatchAction"
Validate= "false" >
<forward name= "Display" path= "/batchlist.jsp"/>
<forward name= "Delete" path= "/batchlist.jsp"/>
</action>

</action-mappings>

<message-resources parameter= "Com.batch.delete.struts.ApplicationResources"/>
</struts-config>

Here is the page content:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%@ taglib uri= "Http://struts.apache.org/tags-bean" prefix= "Bean"%>
<%@ taglib uri= "http://struts.apache.org/tags-html" prefix= "html"%>
<%@ taglib uri= "http://struts.apache.org/tags-logic" prefix= "logic"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>batch_delete</title>
<script type= "Text/javascript" src= "Js/check.js" ></script>
<body>
<center>
Batch Delete Database Data Sample Display
<form action= "Batch.do?batch=deleteselect" name= "MyForm"
Method= "POST" >
<table style= "Border-collapse:collapse" border= "1"
Bordercolor= "#1E90FF" height= "cellpadding=" "3" >
<tr style= "background: #87CEFA;" >
<td>
&nbsp;
</td>
<td>
Id
</td>
<td>
Name
</td>
<td>
Password
</td>
</tr>
<logic:iterate id= "li" name= "list" >
<tr>
<td>
<input type= "checkbox" name= "name" value= "${li.id}" >
</td>
<td>
${li.id}
</td>
<td>
${li.name}
</td>
<td>
${li.password}
</td>
</tr>
</logic:iterate>
<tr>
&LT;TD colspan= "4" >
<input type= "button" value= "SelectAll" onclick= "Checkall ()" >
&nbsp;
<input type= "Submit" value= "Deleteselect" >
&nbsp;
<input type= "reset" value= "Resetselect" >
</td>
</tr>
</table>
</form>
</center>
</body>

JSP page introduces a JS file:

function Checkall () {
for (var i = 0; i < myForm.name.length; i++) {
Myform.name[i].checked=true;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.