String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs为你的数据库的
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你\");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
5. jsp查询数据库代码 这种方法并不是很常用,只是页面的相关操作,代码太多,所以中间省略了部分 <%Object userUID=session.getAttribute("USERID");String uid=""; if(userUID!=null){ uid=(String)userUID; }else{ response.sendRedirect("../Public/loseSession.jsp");} try{String today=DateUtils.getInstance().getToday();String userIP=request.getRemoteAddr();String sql="";String getPage=request.getParameter("toPage"); sql=cu.exchange(request.getParameter("sql")); if (sql==null||sql.equals("")){String getDepId="";String getPoliticalPosition="";String getTechnicalPosition="";String getPoliticalLevel="";String getTechnicalLevel="";String getDegreeCode="";String getGrade="";String getLongevity=""; String getAllowance="";long depId=0;long degreeCode=0;long politicalPosition=0;long technicalPosition=0;long politicalLevel=0;long technicalLevel=0;long grade=0;long longevity=0;long allowance=0;String[] postID=null;String post="";String userID="";String userName="";String address="";getDepId=request.getParameter("depId");depId=Long.parseLong(getDepId.trim()); getDegreeCode=request.getParameter("degreeCode");if (getDegreeCode!=null) {degreeCode=Long.parseLong(getDegreeCode.trim());}getPoliticalPosition=request.getParameter("politicalPosition");if (getPoliticalPosition!=null) {politicalPosition=Long.parseLong(getPoliticalPosition.trim()); }post=request.getParameter("post");if(post!=null&&post.trim().equals("1")) postID=request.getParameterValues("dyourlocation");getTechnicalPosition=request.getParameter("technicalPosition");if (getTechnicalPosition!=null) {technicalPosition=Long.parseLong(getTechnicalPosition.trim()); }getPoliticalLevel=request.getParameter("politicalLevel");if (getPoliticalLevel!=null) { politicalLevel=Long.parseLong(getPoliticalLevel.trim()); } getTechnicalLevel=request.getParameter("technicalLevel");if (getTechnicalLevel!=null) {technicalLevel=Long.parseLong(getTechnicalLevel.trim()); } getGrade=request.getParameter("grade");if (getGrade!=null) {grade=Long.parseLong(getGrade.trim()); } getLongevity=request.getParameter("longevity");if (getLongevity!=null) {longevity=Long.parseLong(getLongevity.trim()); } getAllowance=request.getParameter("allowance");if (getAllowance!=null) { allowance=Long.parseLong(getAllowance.trim());}userID=ParamUtils.getParameter(request,"userID");//用户代码userName=ParamUtils.getParameter(request,"userName");//用户名称address=ParamUtils.getParameter(request,"address");//得到要转入的页面 sql="select org_user.* from org_user,org_detail where org_user.user_id=org_detail.user_id";if (depId!=0){ sql=sql+" and org_user.department_id="+depId+"";} if (userID!=null&&userID!="") { sql=sql+" and org_user.user_id='"+userID+"'";} if (userName!=null&&userName!=""){ sql=sql+" and org_user.name like '%"+userName+"%'";} if (address!=null&&address!="") { sql=sql+" and org_user.address like '%"+address+"%'"; }if(post!=null&&!post.trim().equals("0")) { sql=sql+" and org_detail.post in ("; for(int i=0;i0) { if (cnt%pageSize==0) cntPage=cnt/pageSize; else cntPage=cnt/pageSize+1; } else cntPage=0; if (getPage==null) { getPage="1"; curPage=1; } else curPage=Integer.parseInt(getPage.trim());%>