import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class oraclejdbc { public static void main(String[] args) { //實例化鍵盤輸入 Scanner input = new Scanner(System.in); //提示輸入用戶名 System.out.println("請輸入用戶名:"); String loginid=input.next(); //提示輸入密碼 System.out.println("請輸入密碼:"); String password=input.next(); //數據庫名和登入密碼 String driver="oracle.jdbc.driver.OracleDriver"; String url="jdbc:oracle:thin:@localhost:1521:ORCL"; String user = "epet"; String pwd = "123456"; Connection con=null; PreparedStatement ps=null; ResultSet rs=null; try { Class.forName(driver); con=DriverManager.getConnection(url,user,pwd); //編寫sql語句 String sql="select * from master where loginid=? and password=?"; //得到結果集 ps=con.prepareStatement(sql); ps.setString(1, loginid); ps.setString(2, password); rs = ps.executeQuery(); if(rs.next()){ System.out.println("登入成功!!!"); }else{ System.out.println("用戶名或密碼錯誤,登入失敗!!!"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //依次關閉資源 try { if(rs!=null){ rs.close(); } if(ps!=null){ ps.close(); }if(con!=null){ con.close(); } }catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }