jsp数据库代码怎么写( 二 )


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());%>


6. 在jsp页 面中对数据库的增删改代码怎么写,需要在jsp页面中写java语 public int insertArticle(Article article){//添加 try {Connection connection =null;PreparedStatement preparedStatement=null;Article article=null;connection=JDBCUtils.getConnection(); String sql="insert into article (title,content,publishTime,typeId,userId) values(?,?,?,?,?)"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1, article.getTitle()); preparedStatement.setString(2, article.getContent()); preparedStatement.setString(3, article.getPublishTime()); preparedStatement.setInt(4, article.getTypeId()); preparedStatement.setInt(5, article.getUserId()); return preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return 0; }finally{ JDBCUtils.release(connection, preparedStatement, null); } } public Article queryArticle(int articleId) { Connection con = JDBCUtils.getConnection(); ; PreparedStatement ps = null ; Article article=null; ResultSet rs = null; String sql = "select * from article where articleId='" + articleId + "'"; try { ps=con.prepareStatement(sql); rs = ps.executeQuery(sql); while (rs.next()) { article= new Article(); article.setArticleId(rs.getInt(1)); article.setTitle(rs.getString(2)); article.setContent(rs.getString(3)); article.setPublishTime(rs.getString(4)); article.setUserId(rs.getInt(5)); article.setTypeId(rs.getInt(6)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return article; }public int deleteArticle(int articleId){//删除 Connection conn=null; PreparedStatement pstmt=null; try { String sql="delete from article where articleId='" + articleId+ "'"; conn=JDBCUtils.getConnection(); pstmt=conn.prepareStatement(sql); int rtn=pstmt.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCUtils.release(conn, pstmt, null); } return 0; } public int updateArticle(Article article){//更新 Connection conn=null; PreparedStatement pstmt=null; int articleId=article.getArticleId(); try { String sql="update article set title=?,content=?,publishTime=?,typeId=? where articleId='" + articleId+ "'"; conn=JDBCUtils.getConnection(); pstmt=conn.prepareStatement(sql); pstmt.setString(1, article.getTitle()); pstmt.setString(2, article.getContent()); pstmt.setString(3, article.getPublishTime()); pstmt.setInt(4, article.getTypeId()); int rtn=pstmt.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCUtils.release(conn, pstmt, null); } return 0; }你自己改改数据bean就能用了,jdbc的连接你自己写写 。