package myjdbc;
import java.sql.*;
import java.util.Scanner;
public class Day1 {
public static void main(String[] args){
Scanner sc=new Scanner (System.in);
System.out.println("请输入用户名");
String username=sc.nextLine();
System.out.println("请输入密码");
String password=sc.nextLine();
//加载驱动
//获得与数据库的连接
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "hr", "hr");
stm = conn.createStatement();
//发送sql语句
String sql="select * from t_user where username = '"+username+"' and password = '"+password+"';";
rs = stm.executeQuery(sql);
if(rs.next()){
System.out.println("登陆成功");
}else{
System.out.println("登录失败");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(stm!=null)
stm.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}