package text6; import java.sql.*; public class inquire { Connection con; Statement cmd; ResultSet rs; private String url="jdbc:mysql://127.0.0.1:3306/student"; private String driver="com.mysql.jdbc.Driver"; public boolean validate (String uname,String upwd) { boolean flag=false; try { Class.forName(driver); con=DriverManager.getConnection(url,"root",""); cmd=con.createStatement(); String sql="select *from users"; rs=cmd.executeQuery(sql); if(rs.next()) if(rs.getString(1).equals(upwd))//数据库 flag=true; con.close(); }catch(Exception e) { flag=false; } return flag; } }改错
时间: 2024-03-17 16:45:25 浏览: 123
在该代码中,缺少一个右括号,应该在最后一行的 catch 语句块中加上一个右括号。同时,还需要在 catch 语句块中加上打印异常信息的代码,以便发现程序运行时可能出现的错误。以下是修改后的代码:
```java
package text6;
import java.sql.*;
public class inquire {
Connection con;
Statement cmd;
ResultSet rs;
private String url = "jdbc:mysql://127.0.0.1:3306/student";
private String driver = "com.mysql.jdbc.Driver";
public boolean validate(String uname, String upwd) {
boolean flag = false;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, "root", "");
cmd = con.createStatement();
String sql = "select * from users";
rs = cmd.executeQuery(sql);
if (rs.next())
if (rs.getString(1).equals(upwd))//数据库
flag = true;
con.close();
} catch (Exception e) {
flag = false;
System.out.println("发生异常:" + e.getMessage());
}
return flag;
}
}
```
阅读全文