如何用jdbc连接数据库。查询到数据,在jsp页面上显示,给出参考代码。谢谢

解决方案 »

  1.   

    http://www.enet.com.cn/article/2007/0727/A20070727746889.shtml
      

  2.   

    package com.hjf.web.view; public class PageUtil ...{ 
        private int pageSize;//每页显示的条数 
        private int recordCount;//总共的条数 
        private int currentPage;//当前页面 
        public PageUtil(int pageSize, int recordCount, int currentPage) ...{ 
                this.pageSize = pageSize; 
                this.recordCount = recordCount; 
                setCurrentPage(currentPage); 
            } 
             //构造方法 
            public PageUtil(int pageSize, int recordCount) ...{ 
                this(pageSize, recordCount, 1); 
            } 
            //总页数 
            public int getPageCount() ...{ 
                int size = recordCount/pageSize;//总条数/每页显示的条数=总页数 
                int mod = recordCount % pageSize;//最后一页的条数 
                if(mod != 0) 
                    size++; 
                return recordCount == 0 ? 1 : size; 
            } 
            //包含,起始索引为0 
            public int getFromIndex() ...{ 
                //System.out.println("from index:"+(currentPage-1) * pageSize); 
                return (currentPage-1) * pageSize; 
            } 
            //不包含 
            public int getToIndex() ...{ 
                //System.out.println("to index:"+Math.min(recordCount, currentPage * pageSize)); 
                return  Math.min(recordCount, currentPage * pageSize); 
            } 
            //得到当前页 
            public int getCurrentPage() ...{ 
                return currentPage; 
            }//设置当前页 
            public void setCurrentPage(int currentPage) ...{ 
                int validPage = currentPage <= 0 ? 1 : currentPage; 
                validPage = validPage > getPageCount() ? getPageCount() : validPage; 
                this.currentPage = validPage; 
            }//得到每页显示的条数 
            public int getPageSize() ...{ 
                return pageSize; 
            }//设置每页显示的条数 
            public void setPageSize(int pageSize) ...{ 
                this.pageSize = pageSize; 
            }//得到总共的条数 
            public int getRecordCount() ...{ 
                return recordCount; 
            }//设置总共的条数 
            public void setRecordCount(int recordCount) ...{ 
                this.recordCount = recordCount; 
            } 

    -------------------------------------------- 
    下面的代码是放在jsp里面的 
    -------------------------------------------- 
    <% 
    PublishersDAO dao = PublishersDAO.getInstance(); 
    List records = dao.getModels(); 
    String pageStr = request.getParameter("page"); 
    int currentPage = 1; 
    if (pageStr != null) 
    currentPage = Integer.parseInt(pageStr); 
    PageUtil pUtil = new PageUtil(10, records.size(), currentPage); 
    currentPage = pUtil.getCurrentPage(); 
    %> 
    -----------下面这个是放在有变量的上面--------------------------------- 
    <% 
    for (int i = pUtil.getFromIndex(); i < pUtil.getToIndex(); i++) { 
    PublisherModel model = (PublisherModel) records.get(i); 
    %> 
    中间是删除修改之类的代码 
    <%}%> 
    ------------这个是结尾的-------------------------- 
    <tr><td width=100% bgcolor="#eeeeee" colspan=4 align="center"> 
    记录总数<%=pUtil.getRecordCount()%>条 当前页/总页数<%=currentPage%> 
    /<%=pUtil.getPageCount()%>每页显示<%=pUtil.getPageSize()%>条 
    <a href="publishers.jsp?page=1">首页</a> 
    <a href="publishers.jsp?page=<%=(currentPage - 1)%>">上页</a> 
    <a href="publishers.jsp?page=<%=(currentPage + 1)%>">下页</a> 
    <a href="publishers.jsp?page=<%=pUtil.getPageCount()%>">末页</a> 
    </td></tr>本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/nihaozhangchao/archive/2010/01/05/5134304.aspx
      

  3.   

    google 一下,大把大把的,浪费100分
      

  4.   

    首先要有数据库的JAR包 然后才能连,这个连接程序网上有,自己去搜一下
      

  5.   

    使用jstl sql标签<%@ page contentType="text/html; charset=GBK"%> 
        <%@   taglib   prefix="c"   uri="/WEB-INF/c.tld"   %>  
        <%@ taglib prefix="x" uri="/WEB-INF/x.tld" %> 
    <%@ taglib prefix="sql" uri="/WEB-INF/sql.tld" %> <html xmlns="http://www.w3.org/1999/xhtml"/> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 
    <title>Insert title here</title> 
    </head> 
    <body> 
    <form action=""> 
    <c:import url="www.g.cn" var="thisPage"> </c:import> <input type="button" value="button" /> 
    <sql:setDataSource 
    var="mdb" 
    driver="oracle.jdbc.driver.OracleDriver" 
    url="jdbc:oracle:thin:@192.168.2.12:1521:ecom" 
    user="fjyd3" 
    password="fjyd3" 
    /> 
    <sql:query 
    var="query" 
    dataSource="mdb" 
    sql="select * from flow_cusdb"> 
    </sql:query> 
    <c:forEach var="row" items="${query.rows}"> 
    hello !!jdbc 
    </c:forEach> 
    <table> 
    <c:forEach var="row" items=""> 
    </c:forEach> 
    </table> 
    </form> 
    </body> 
    </html> 
      

  6.   

    哎,还是自己钻研吧,JDBC连数据库不难,直接写在JSP页面感觉不安全
      

  7.   

    一般是后台servlet连接数据库,查询到结果后用request或Ajax方法传递给前台显示
      

  8.   

    1、搞清楚是什么数据库,找到对应的 jdbc 驱动包;
    2、看 jdbc 驱动的说明文档,了解连接 uri 的构成要素;
    3、按构成要素拼好连接 jdbc 的 uri;
    4、搞清楚是否还需要一些额外配置才能使用 jdbc 驱动方式与数据库建立连接;当以上的事项都了解清楚了,你就知道如何使用 jdbc 连接数据库了。
      

  9.   

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;public class DBCon {
    public static String driver="oracle.jdbc.driver.OracleDriver";
    public static String url="jdbc:oracle:thin:@192.168.0.174:1521:ORCL";
    public static String user="scott";
    public static String pwd="tiger";

    public static Connection getConnection(){
    try {
    Class.forName(driver);
    Connection con=DriverManager.getConnection(url, user, pwd);
    System.out.println("con success");
    return con;
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return null;
    }

    public static void close(Connection con,Statement stm,ResultSet rs){
    if(con!=null){
    try {
    con.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if(stm!=null){
    try {
    stm.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if(rs!=null){
    try {
    rs.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    public static void main(String[] args) {
    DBCon.getConnection();
    }
    }
    增删改查  csdn下载区搜一个  太多了
      

  10.   

    弱弱的问一下 楼主你会什么? 知道java代码怎么写吗?