new String(txtpwd.getpassword())是什么意思
时间: 2024-06-02 22:10:41 浏览: 8
`txtpwd.getpassword()` 是获取密码输入框中的密码字符串。然后 `new String(...)` 创建了一个新的字符串对象,其内容就是这个密码字符串。这样做的原因是为了避免密码字符串在内存中被其他程序或者人恶意获取和利用。使用 `new String(...)` 创建新的字符串对象,可以使得原始的密码字符串在不再需要时被及时地回收和释放。
相关问题
try { String connectDB = "jdbc:sqlserver://localhost:1433;DatabaseName=DB_201623010355;encrypt=false"; String user = "sa"; // 这里只要注意用户名密码不要写错即可 String password = "123456789"; Connection con = DriverManager.getConnection(connectDB, user,password); if(kind.equals("管理员")) { sql="select * from Admin where admin_ID= '"+strName+"' and admin_password='"+strPwd+"'"; } else { sql="select * from subcs where subcs_ID= '"+strName+"' and subcs_password='"+strPwd+"'"; } Statement stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { if(kind.equals("管理员")) { new Manager().setVisible(true); dispose(); } else { new Student(strName).setVisible(true); dispose(); } } else { JOptionPane.showMessageDialog(null,"用户名或密码不正确","消息对话框",JOptionPane.WARNING_MESSAGE); txtPwd.setText(""); txtName.requestFocus(); } } catch(SQLException ex) { ex.printStackTrace(); } finally { try { rs.close(); stmt.close(); conn.close(); }catch(Exception ex) { } } } });
这段代码主要用于 Java 中使用 JDBC 连接数据库并进行用户登录的操作。具体步骤如下:
1. 定义连接数据库的 URL、用户名和密码,其中 URL 中包含了数据库的 IP 地址、端口号和数据库名称,例如 `"jdbc:sqlserver://localhost:1433;DatabaseName=DB_201623010355;encrypt=false"`。
2. 通过 `DriverManager.getConnection()` 方法创建一个 `Connection` 对象,表示与数据库建立的连接。
3. 根据用户类型(管理员或学生),拼接 SQL 查询语句,并调用 `conn.createStatement().executeQuery()` 方法执行查询,返回一个 `ResultSet` 对象。
4. 如果查询结果中有匹配的记录,则打开相应的窗口,否则提示用户名或密码不正确。
5. 在 `finally` 块中关闭 `ResultSet`、`Statement` 和 `Connection` 对象,释放资源。
需要注意的是,该段代码存在 SQL 注入漏洞,应该使用预编译语句或参数化查询来避免此类攻击。
protected void Page_Load(object sender, EventArgs e) { this.Title = "网上服装店"; if (!IsPostBack) { if (Session["UserId"] == null) { Alert.AlertAndRedirect("对不起您还没有登录", "Default.aspx"); } else { } } } /// <summary> /// 修改密码 /// </summary> private void UPpwd() { string Username = Session["UserId"].ToString(); try { //实例化对象 Model.Users en = new Model.Users(); en.pwd = txtpwd2.Text.ToString(); en.id = int.Parse(Username); //业务逻辑层 BLL.UsersBusiness busi = new BLL.UsersBusiness(); busi.UpdatePassword(en); //将提示信息赋值到页面的label控件上面 LabelWarningMessage.Text = "修改成功!"; } catch { LabelWarningMessage.Text = "修改失败!"; } } /// <summary> /// 检验原来的密码 /// </summary> private void chkpwd() { SqlDataReader dr; dr = DAL.SQLDB.ExecuteReader("select * from Users where id='" + Session["UserId"].ToString() + "' and pwd='" + txtpwd1.Text + "'"); if (dr.Read()) { UPpwd(); } else { LabelWarningMessage.Text = "原密码不正确!"; } } protected void Button1_Click(object sender, EventArgs e) { chkpwd(); }
这段代码是一个C#的ASP.NET网页代码,主要实现了网上服装店的修改密码功能。在页面加载时,会检查用户是否已经登录,如果没有则提示用户登录。在修改密码功能中,首先会检验输入的原密码是否正确,如果正确则更新数据库中的密码,否则提示用户原密码不正确。该页面使用了三个函数,分别是Page_Load、UPpwd和chkpwd,其中Page_Load是页面加载函数,用于初始化页面;UPpwd是更新密码函数,用于更新数据库中的密码;chkpwd是检验密码函数,用于检验输入的原密码是否正确。在修改密码按钮被点击时,会调用chkpwd函数进行密码检验。