谁能给我一份JSP对sql2005数据库的一份增删改查的详细代码,要份简单点的。急需.最好带上详细的注释!

解决方案 »

  1.   

     try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    System.out.println("load error!");
    }
        String URL = "jdbc:sqlserver://localhost:1433;DatabaseName = myDatabase";
        String USER = "sa";  
        String PASSWORD = "123";  
        
        try {
    conn = DriverManager.getConnection(URL,USER,PASSWORD);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    System.out.println("connection error!");
    }

    if (conn == null) {
        System.out.println("connection == null");
    }剩下的自己写
      

  2.   

    我有详细的  你在csdn下载页 搜索《jsp技术用户管理案例》就可以找到了  是我写的 你去下载把 数据库是sqlserver2005的。
      

  3.   

    public class JDBConnection {
        private final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_BlodMay";
        private final String userName = "sa";
        private final String password = "";
        private Connection con = null;
    //通过构造方法加载数据库驱动
        static {
            try {
                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
            } catch (Exception ex) {
                System.out.println("数据库加载失败");
            }
        }
    //创建数据库连接
        public boolean creatConnection() {
            try {
                con = DriverManager.getConnection(url, userName, password);
                con.setAutoCommit(true);        } catch (SQLException e) {
                System.out.println(e.getMessage());
                System.out.println("creatConnectionError!");
            }
            return true;
        }
    //对数据库的增加、修改和删除的操作
        public boolean executeUpdate(String sql) {
             if (con == null) {
                creatConnection();
            }
            try {
                Statement stmt = con.createStatement();
                int iCount = stmt.executeUpdate(sql);
                System.out.println("操作成功,所影响的记录数为" + String.valueOf(iCount));
        return true;
            } catch (SQLException e) {
                System.out.println(e.getMessage());
        return false;
            }   
        }
    //对数据库的查询操作
        public ResultSet executeQuery(String sql) {
            ResultSet rs;
            try {
                if (con == null) {
                    creatConnection();
                }
                Statement stmt = con.createStatement();
                try {
                    rs = stmt.executeQuery(sql);
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                    return null;
                }
            } catch (SQLException e) {
                System.out.println(e.getMessage());
                System.out.println("executeQueryError!");
                return null;
            }
            return rs;
        }
    }
      

  4.   

    public class BaseDao
    {
    public final static String URL = "jdbc:sqlserver://localhost:1433;DataBaseName = exam";
    public final static String Driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    public final static String DBname = "sa";
    public final static String DBpass = "welcome";

    public Connection getCon() throws ClassNotFoundException,SQLException
    {
    Class.forName(Driver); //注册驱动
    Connection con = DriverManager.getConnection(URL, DBname, DBpass);
    return con;
    }
    public void closeAll(Connection con,PreparedStatement ps,ResultSet rs) throws SQLException
    {
    if(rs != null) //如果rs不为空,关闭 rs
    {
    try
    {
    rs.close();
    }
    catch (SQLException e)
    {
    e.printStackTrace();
    System.out.println("这是第一处rs的异常"+e);
    }

    }
    if(ps != null) //如果ps不为空,关闭ps
    {
    try
    {
    ps.close();
    }
    catch (SQLException e)
    {
    e.printStackTrace();
    System.out.println("这是第二处ps的异常"+e);
    }
    }
    if(con != null)
    {
    try
    {
    con.close();
    }
    catch (SQLException e)
    {
    e.printStackTrace();
    System.out.println("这是第三处con的异常"+e);
    }
    }
    }
             //万能方法,增删改
    public int executSQL(String psql,String[] param) throws SQLException
    {
    Connection con = null;
    PreparedStatement ps = null;
    int num = 0;
    try
    {
    con = getCon();
    ps = con.prepareStatement(psql);
    if(param != null)
    {
    for(int i = 0;i<param.length;i++)
    {
    ps.setString(i+1, param[i]);
    }
    }
    num = ps.executeUpdate();
    }
    catch (ClassNotFoundException e)
    {
    e.printStackTrace();
    System.out.println("这是第四处异常"+e);
    // TODO: handle exception
    }
    catch(SQLException es)
    {
    es.printStackTrace();
    System.out.println("这是第五处异常"+es);
    }
    finally
    {
    closeAll(con, ps, null);
    }
    return num;
    }
    }
      

  5.   

    访问数据库怎么会在JSP页面上?
      

  6.   

    写在特定类里 在jsp页面上调用岂不是更好么...
      

  7.   

    vpublic class BaseDao
    {
    public final static String URL = "jdbc:sqlserver://localhost:1433;DataBaseName = exam";
    public final static String Driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    public final static String DBname = "sa";
    public final static String DBpass = "welcome";public Connection getCon() throws ClassNotFoundException,SQLException
    {
    Class.forName(Driver); //注册驱动
    Connection con = DriverManager.getConnection(URL, DBname, DBpass);
    return con;
    }
    public void closeAll(Connection con,PreparedStatement ps,ResultSet rs) throws SQLException
    {
    if(rs != null) //如果rs不为空,关闭 rs
    {
    try
    {
    rs.close();
    }
    catch (SQLException e)
    {
    e.printStackTrace();
    System.out.println("这是第一处rs的异常"+e);
    }}
    if(ps != null) //如果ps不为空,关闭ps
    {
    try
    {
    ps.close();
    }
    catch (SQLException e)
    {
    e.printStackTrace();
    System.out.println("这是第二处ps的异常"+e);

    }
    if(con != null)
    {
    try
    {
    con.close();
    }
    catch (SQLException e)
    {
    e.printStackTrace();
    System.out.println("这是第三处con的异常"+e);
    }
    }
    }
      //万能方法,增删改
    public int executSQL(String psql,String[] param) throws SQLException
    {
    Connection con = null;
    PreparedStatement ps = null;
    int num = 0;
    try
    {
    con = getCon();
    ps = con.prepareStatement(psql);
    if(param != null)
    {
    for(int i = 0;i<param.length;i++)
    {
    ps.setString(i+1, param[i]);
    }
    }
    num = ps.executeUpdate();
    }
    catch (ClassNotFoundException e)
    {
    e.printStackTrace();
    System.out.println("这是第四处异常"+e);
    // TODO: handle exception
    }
    catch(SQLException es)
    {
    es.printStackTrace();
    System.out.println("这是第五处异常"+es);
    }
    finally
    {
    closeAll(con, ps, null);
    }
    return num;
    }
    }
      

  8.   


    <%
                Connection conn=null;
                Statement statement=null;
                ResultSet rs=null
    try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                String URL = "jdbc:sqlserver://localhost:1433;DatabaseName = myDatabase";
                String USER = "sa";  
                String PASSWORD = "123";  
                conn = DriverManager.getConnection(URL,USER,PASSWORD);
                statement=conn.createstatement();
                .........
    }catch(Exception e){
       
    }finally{
       rs.close();
       statement.close();
       conn.close();
    }%>
      

  9.   

    哎呀我的妈呀~~都只写~BaseDao...
    来点真的行不??
      

  10.   

    拿来主义。呵呵,其实这个问题很简单的,还是在网上搜一搜吧。使用简单的jdbc就行了。
      

  11.   

     String suser="sa";
          String spassword="";
          String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎   
          String connectDB="jdbc:sqlserver://localhost:1433;DatabaseName=*****";//数据源
          ResultSet rs =null;
          String sql = null;
         %>
          
         <% 
          try
          {
             Class.forName(JDriver);//加载数据库引擎,返回给定字符串名的类 
             Connection con=DriverManager.getConnection(connectDB,suser,spassword);//连接数据库对象
             Statement stmt=con.createStatement();//创建SQL命令对象
             sql ="select reader_id from keys where reader_id='"+id+"' and password ='"+bpassword+"'";
             rs = stmt.executeQuery(sql);
           if(rs.next())
           {
             flag = true;
             session.setAttribute("uname",rs.getString(1));
           }
           rs.close();
           stmt.close();
           con.close();
          }catch(Exception e)
          {
            e.printStackTrace();
          }     
      

  12.   

    兄弟够懒的。连数据库。我想你逗我们玩的吧。随便的破JAVA书籍上面只要提连接数据库都有现成的代码自己敲吧。package dao;import java.sql.*;
    import java.util.*;public class NewsDaoImpl implements NewsDao{ public int addNews(String biaoti, String neirong) {
    // TODO Auto-generated method stub

    PreparedStatement pstmt=null;
    int count=0;
    try{
    Connection conn=BaseDao2005.getConnection();
    String sql="insert into News(biaoti,neirong) values(?,?)";
    pstmt=conn.prepareStatement(sql);
    pstmt.setString(1, biaoti);
    pstmt.setString(2, neirong);
    count=pstmt.executeUpdate();
    }catch(SQLException el){
    el.printStackTrace();
    }finally{
    BaseDao2005.coseConnection();

    }

    return count;

    } public void delteNews(String id) {
    // TODO Auto-generated method stub
    try{
    Connection conn=BaseDao2005.getConnection();
    Statement stmt=conn.createStatement();
    String sql="delete from News where id="+id;
    stmt.execute(sql);
    }catch(SQLException el){
    el.printStackTrace();
    }finally{
    BaseDao2005.coseConnection();
    }

    }
    private News parseNews(ResultSet rs) throws SQLException{

    News news=new News();
    news.setId(rs.getInt("id"));
    news.setBiaoti(rs.getString("biaoti"));
    news.setNeirong(rs.getString("neirong"));
    news.setTime(rs.getDate("time"));
    return news;



    }
    public List<News> getNews(){
    List<News>list=new ArrayList<News>();
    int recordCount=getRecordCount();
    try{Connection conn=BaseDao2005.getConnection();
    Statement stmt=conn.createStatement();
    String sql="select top 15 * from News where id not in (select top "+(recordCount-5)+"  id from News order by id asc )order by id desc";
    ResultSet rs=stmt.executeQuery(sql);
    News news;
    while(rs.next()){

    news=parseNews(rs);
    list.add(news);
    }
    }catch(SQLException el){
    el.printStackTrace();
    }finally{
    BaseDao2005.coseConnection();
    }
    return list;

    }

    public List<News> getNews2(String id){
    List<News>list=new ArrayList<News>();
    try{Connection conn=BaseDao2005.getConnection();
    Statement stmt=conn.createStatement();
    String sql="select * from News where id="+id;
    ResultSet rs=stmt.executeQuery(sql);
    News news;
    while(rs.next()){

    news=parseNews(rs);
    list.add(news);
    }
    }catch(SQLException el){
    el.printStackTrace();
    }finally{
    BaseDao2005.coseConnection();
    }
    return list;

    }

    public List<News> getNews(String requestPage) {
    Connection conn=BaseDao2005.getConnection();//连接数据库
     int page=1; //页数
     int pageSize=15; //显示条数
     int pageCount=0; //总页数
     int recordCount=0; //总条数
     String sqlStr=null; //SQL语句
     
     try{
    page=Integer.parseInt(requestPage);
    }catch(NumberFormatException e){       //
    page=1;
    }
    //数据库中图书信息的总记录数
    recordCount=getRecordCount();
    //设定总页数
    if(recordCount <1)
    pageCount=0;
    else
    pageCount=(int)(recordCount-1)/pageSize+1;     //

    if(page<1)
    page=1;
    else if(page>pageCount)
    page=pageCount;
    //根据用户选择显示的页码动态形成SQL语句

    if(page==1){
    sqlStr="select top 15 * from News where id not in (select top 0  id from News order by id asc )order by id desc";   //

    }else{
    sqlStr="select top "+pageSize+" * from News where id not in (select top "+(page-1)*pageSize+" id from News order by id asc )order by id desc";
    }
    List<News> list=new ArrayList<News>();
    try{
    Statement stmt=conn.createStatement();

    ResultSet rs=stmt.executeQuery(sqlStr);
    News News;
    while(rs.next()){

    News=parseNews(rs);
    list.add(News);
    }
    }catch(SQLException el){
    el.printStackTrace();
    }finally{
    BaseDao2005.coseConnection();
    }
    return list;
    }

    public int updateNews(String id, String biaoti, String neirong) {
    Connection conn=BaseDao2005.getConnection();
     int shu=-1;
     Statement st=null;
        String Sql="update News set biaoti='"+biaoti+"',neirong='"+neirong+"' where id='"+id+"'";
        try{
        st=conn.createStatement();
        shu=st.executeUpdate(Sql);
        }catch(SQLException e){
        e.printStackTrace();
        }
    return shu;
    }
    public static  int getRecordCount(){

    String sqlStr="select count(*) from News";
    int r=0;
    try{
    Connection conn=BaseDao2005.getConnection();
    Statement stmt=conn.createStatement();
    ResultSet rs=stmt.executeQuery(sqlStr);

    if(rs.next())
    r=rs.getInt(1);
    }catch(Exception e){
    e.printStackTrace();
    }

    return r;
    }
    }
      

  13.   

    crud很简单的,你试着自己写一下啦