StringBuffer 拼接sql语句


<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Hello</title>
</head>
<body>
<% 
	Connection conn=null; 
	ResultSet rs=null;
	PreparedStatement ps=null;
	String mobile="13588844873";
	try{
		Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
		String url="jdbc:oracle:thin:@localhost:1521:orcl";
		conn=DriverManager.getConnection(url,"zhang","zhang");
	//	conn.setAutoCommit(false);
		StringBuffer str=new StringBuffer(" where 1=1 ");
		str.append(" and mobile like '%").append(mobile+"%").append("'");
		String sql="select * from ins "+str.toString()+"";
		ps=conn.prepareStatement(sql);
	//	ps.setString(1,mobile);
		rs=ps.executeQuery();
		while(rs.next()){
			out.println("下面是使用StringBuffer的结果");
			out.println(rs.getString("createtime"));
			out.println(rs.getString("mobile"));
			out.println("<br/>");
		}
	}catch(Exception e){
		e.printStackTrace();
	}
	 
 
 %>

</body>
</html>






<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>哈哈</title>
</head>
<body>&nbsp; 
<% 
	Connection conn=null; 
	ResultSet rs=null; 
	PreparedStatement ps=null; 
	String mobile="135";
	try{
		Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
		String url="jdbc:oracle:thin:@localhost:1521:orcl";
		conn=DriverManager.getConnection(url,"zhang","zhang");
		conn.setAutoCommit(false);
		StringBuffer str=new StringBuffer("where 1=1");
		str.append("and mobile like '%").append(mobile+"%").append("'");
		String sql="select * from ins "+str.toString()+"";
		ps=conn.prepareStatement(sql);
		rs=ps.executeQuery();
		conn.commit();
		while(rs.next()){
			out.println("StringBuffer练习");
			out.println(rs.getString("createtime"));
			out.println(rs.getString("mobile"));
			out.println("<br/>");
		}
	}catch(Exception e){
		conn.rollback();
		e.printStackTrace();
	}finally{
		if(rs!=null){
			rs.close();
		}
		if(ps!=null){
			ps.close();
		}
		if(conn!=null){
			conn.close();
		}
	}
	 
 
 
 %>
</body>
</html>

你可能感兴趣的:(java,oracle,sql,jdbc,mobile)