jsp连接SQL数据库

    String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    try{
        Class.forName(driver);
    }
    catch(Exception e){
        out.write("ERR1:"+e.getMessage());
        return;
    }
    String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=BBS";
    String user="sa";
    String pwd="123456";
    Connection conn = null;
    try{
        conn = DriverManager.getConnection(url, user, pwd);
        String sql = "insert into BBS_User values(?,?,?,?,?,?)";
        PreparedStatement pStatement = conn.prepareStatement(sql);
        pStatement.setString(1, "00001");
        pStatement.setString(2, "123");
        pStatement.setString(3, "tim");
        pStatement.setString(4, "3196");
        pStatement.setString(5, "male");
        pStatement.setString(6, "1");
        int n = pStatement.executeUpdate();
    }
    catch(SQLException e){
        out.write("ERR2:"+e.getMessage());
        return;
    }
    out.write("okk");

你可能感兴趣的:(jsp连接SQL数据库)