|
相信大家对平凡的查询数据库都有很深的认识,这里说的是查询的思路是什么的问题。遇到在数据库中查重怎么来做。这里一个例子:“在同一个table中有‘张三’,‘张三33333’,‘张三4444’。那么要求把属于‘张三’的人都找出来并把这些‘张三insert到另外一张表里。’”。对于这个问题是怎么来处理呢?因为数据库中有很多人的名字,最快的方法是什么呢?这里可以采用从数据库中第一个数据取出来,然后再对这些数据进行比较查重。比较完第一个后再比较第二个。这样依次比较。下面我举一个实际的例子。简单用jsp连接数据库查重。
index.jsp
<%@ page import="java.io.*,
java.util.*,
java.sql.*"
%>
<%@ include file="function.jsp"%>
<%@page contentType="text/html; charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="rogId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
</head>
<body>
<table border="1" width="100%">
<tr>
<td width="50%">
<p align="center">id</td>
<td width="50%">
<p align="center">name</td>
</tr>
<%
try{
String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName(sDBDriver);
String sConnStr="jdbc:microsoft:sqlserver://172.31.11.211:1433;DatabaseName=webflow";
String username="sa";
String password="ecseflow2005";
Connection conn = DriverManager.getConnection(sConnStr,username,password);
java.sql.Statement stmt=conn.createStatement();
String sql = "select * from bakkk";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
String str = rs.getString("name").trim();
try{
if(isRepeat22(str.substring(0,2))){
System.out.println("************************");
System.out.println(str);
System.out.println("***********************");
//addStr(rs.getString(1),rs.getString(2));
update111(rs.getString("id"),rs.getString("name"));
}
else{
%>
<%
}
}catch(SQLException ex){
System.out.println(ex.getMessage());
}finally{
try{
if(stmt2 != null) {stmt2.close();
if(conn2 != null) conn2.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
try{
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
%>
</table>
</body>
</html>
function.jsp
<%@ page import="java.util.StringTokenizer,
java.text.SimpleDateFormat,
java.sql.*"%>
<%!
public boolean isRepeat22(String str) throws Exception{
boolean f = false;
try{
String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName(sDBDriver);
String sConnStr="jdbc:microsoft:sqlserver://172.31.11.211:1433;DatabaseName=webflow";
String username="sa";
String password="ecseflow2005";
Connection conn = DriverManager.getConnection(sConnStr,username,password);
java.sql.Statement stmt=conn.createStatement();
// java.sql.Statement stmt=conn.createStatement();
String sql = "select count(*) from bakkk where name like '%"+str+"%'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.getInt(1) > 2){
f = true;
}else{
f = false;
}
}catch(Exception e){
System.out.println(e.getMessage());
}
return f;
}
public void addStr(String str1,String str2) throws Exception{
try{
String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName(sDBDriver);
String sConnStr="jdbc:microsoft:sqlserver://172.31.11.211:1433;DatabaseName=webflow";
String username="sa";
String password="ecseflow2005";
Connection conn2 = DriverManager.getConnection(sConnStr,username,password);
java.sql.Statement stmt2=conn2.createStatement();
String sql2 = "insert into bakkk1 (id,name) values ("+str1+","+str2+")";
stmt2.executeUpdate(sql2);
conn2.commit();
// System.out.println(sql2);
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
public void update111(String str1,String str2) throws Exception{
try{
String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName(sDBDriver);
String sConnStr="jdbc:microsoft:sqlserver://172.31.11.211:1433;DatabaseName=webflow";
String username="sa";
String password="ecseflow2005";
String sql = "update users set UFD6 = '"+str1+"' where NAME like '%"+str2+"%'";
Connection conn = DriverManager.getConnection(sConnStr,username,password);
java.sql.Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
conn.commit();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
%>
基本思路就是这样,代码运行可能会有点小问题,请大家发现指出。 |
|