package TestJDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class JDBCDemo {
private final static String URL="jdbc:oracle:thin:@localhost:1521:xe";
private final static String USERNAME="system";
private final static String PWD="wd1999";
public static void query() { // 查询
Connection connection = null;
Statement stmt = null;
ResultSet rs = null ;
try {
// a.导入驱动,加载具体的驱动类
Class.forName("oracle.jdbc.OracleDriver");// 加载具体的驱动类
// b.与数据库建立连接
connection = DriverManager.getConnection(URL, USERNAME, PWD);
// c.发送sql,执行(增删改、【查】)
stmt = connection.createStatement();
Scanner input= new Scanner(System.in);
System.out.println("请输入用户名:");
String name = input.nextLine() ;
System.out.println("请输入密码:");
String pwd = input.nextLine() ;
String sql = "select count(*) from login1 where uname='"+name+"' and upwd ='"+pwd+"' " ;
// 执行SQL(增删改executeUpdate(),查询executeQuery())
rs = stmt.executeQuery(sql); // 返回值表示 增删改 几条数据
// d.处理结果
int count = -1;
if(rs.next()) {
count=rs.getInt(1);
}
if(count>0) {
System.out.println("登陆成功!");
}else {
System.out.println("登陆失败!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
finally {
try {
if(rs!=null) rs.close();
if(stmt!=null) stmt.close(); // 对象.方法
if(connection!=null)connection.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
query() ;
}
}
就是处理结果这里 我的数据库中一张login1的表中有一个 uname :zs upwd:abc 显示结果一直不正确 经过我的调试发现rs.getInt(1)的值一直为0 按理讲返回的值为应该为1 求求大佬帮忙 小白想了一上午也没改对