在jsp页面连接数据库 并且插入数据(sqlserver数据库)
在线跪求完整的java代码
在此谢过了、

解决方案 »

  1.   

    http://blog.csdn.net/mail_ricklee/article/details/307464 这里介绍的很详细
      

  2.   

    当年我也遇到过同样的问题不过不知道你用的是哪个版本的。如果是2000的话,要装sp3补丁,然后把3个jar包导入到工程,然后就可以用java代码操作数据库了。。如果是sql2005的话,不用装补丁,不过貌似要配置端口还是啥来着,忘记了。
      

  3.   

    public static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";                      //数据库驱动
    public static final String URL="jdbc:sqlserver://localhost:1433;DatabaseName=company";                 //URL
        public static final String NAME="sa";                                                                  //数据库用户名
        public static final String PWD="sa";                                                                   //数据库密码
        
        /**
         * 得到数据库连接
         * @return
         * @throws SQLException
         * @throws ClassNotFoundException
         */
        public Connection getConn() throws SQLException, ClassNotFoundException
        {
         Class.forName(DRIVER);              //获取驱动
         Connection conn=DriverManager.getConnection(URL,NAME,PWD);
         return conn;                 //返回连接对象
        }
     /**
         *执行SQL语句,可以进行增、删、改、查 的操作,不能执行查询 
         * @param conn
         * @param pstmt
         * @param rs
         */
        public int executeSQL(String sql,String[] param)
    {
    int result=0;
    Connection conn=null;
    PreparedStatement ps=null;

    /*处理SQL,执行SQL*/
    try 
    {
    //得到数据库连接
    conn=this.getConn();
    //得到PreparedStatement对象
    ps=conn.prepareStatement(sql);
    if(param!=null)
    {
    //为预编译sql设置参数
    for(int i=0;i<param.length;i++)
    {
    ps.setString((i+1),param[i]);
    }
    }
    //执行语句
    result=ps.executeUpdate();

    catch (ClassNotFoundException e) 
    {
    e.printStackTrace();
    }
    catch (SQLException e) 
    {
    e.printStackTrace();
    }
    finally
    {
    //释放资源
    this.closeAll(conn, ps, null);
    }
    return result;
    }