1.要从键盘录入用户名与密码我们需要使用Scanner类完成操作

2.接收到用户名与密码后,我们需要调用jdbc程序根据用户名与密码查询数据库

User.java

  1. package com.supergroup.domian;
  2.  
  3. public class User{
  4. private String id;
  5. private String usernasme;
  6. private String password;
  7. private String email;
  8. public User() {
  9. super();
  10. }
  11. public User(String id, String usernasme, String password, String email) {
  12. super();
  13. this.id = id;
  14. this.usernasme = usernasme;
  15. this.password = password;
  16. this.email = email;
  17. }
  18. public String getId() {
  19. return id;
  20. }
  21. public void setId(String id) {
  22. this.id = id;
  23. }
  24. public String getUsernasme() {
  25. return usernasme;
  26. }
  27. public void setUsernasme(String usernasme) {
  28. this.usernasme = usernasme;
  29. }
  30. public String getPassword() {
  31. return password;
  32. }
  33. public void setPassword(String password) {
  34. this.password = password;
  35. }
  36. public String getEmail() {
  37. return email;
  38. }
  39. public void setEmail(String email) {
  40. this.email = email;
  41. }
  42. @Override
  43. public String toString() {
  44. return "User [id=" + id + ", usernasme=" + usernasme + ", password="
  45. + password + ", email=" + email + "]";
  46. }
  47.  
  48. }

Operation.java

  1. package com.supergroup.operation;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. import com.supergroup.domian.User;
  11. import com.supergroup.utils.JDBCUtils;
  12.  
  13. public class Operation {
  14. public static User select(User user) throws ClassNotFoundException,
  15. SQLException {
  16. User result = null;
  17. Connection con = null;
  18. Statement st = null;
  19. ResultSet rs = null;
  20. // TODO Auto-generated method stub
  21.  
  22. con = JDBCUtils.getConnection();
  23. PreparedStatement ps=con.prepareStatement("select * from user where username=? and password=?");
  24. ps.setString(1, user.getUsernasme());
  25. ps.setString(2, user.getPassword());
  26. rs=ps.executeQuery();
  27.  
  28. if (rs.next()) {
  29. result = new User(rs.getString(1), rs.getString(2),
  30. rs.getString(3), rs.getString(4));
  31.  
  32. }
  33. JDBCUtils.closeAll(rs, st, con);
  34.  
  35. return result;
  36. }
  37.  
  38. public static User _select(User user) throws ClassNotFoundException,
  39. SQLException {
  40. User result = null;
  41. Connection con = null;
  42. Statement st = null;
  43. ResultSet rs = null;
  44. // TODO Auto-generated method stub
  45.  
  46. con = JDBCUtils.getConnection();
  47. st = con.createStatement();
  48. rs = st.executeQuery("select * from user where username='"
  49. + user.getUsernasme() + "'and password ='" + user.getPassword()
  50. + "'");
  51. if (rs.next()) {
  52. result = new User(rs.getString(1), rs.getString(2),
  53. rs.getString(3), rs.getString(4));
  54.  
  55. }
  56. JDBCUtils.closeAll(rs, st, con);
  57.  
  58. return result;
  59. }
  60.  
  61. }

JDBCUtils.java

  1. package com.supergroup.utils;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ResourceBundle;
  9.  
  10. public class JDBCUtils {
  11. private static String DRIVER;
  12. private static String URL;
  13. private static String UNAME;
  14. private static String PWD;
  15. static {
  16. ResourceBundle rb = ResourceBundle
  17. .getBundle("com.supergroup.utils.JDBC");
  18. DRIVER = rb.getString("DRIVER");
  19. URL = rb.getString("URL");
  20. UNAME = rb.getString("UNAME");
  21. PWD = rb.getString("PWD");
  22.  
  23. }
  24.  
  25. static {
  26.  
  27. try {
  28. Class.forName(DRIVER);
  29. } catch (ClassNotFoundException e) {
  30. // TODO Auto-generated catch block
  31. e.printStackTrace();
  32. }
  33. }
  34.  
  35. public static Connection getConnection() throws SQLException,
  36. ClassNotFoundException {
  37. // TODO Auto-generated method stub
  38.  
  39. return DriverManager.getConnection(URL, UNAME, PWD);
  40.  
  41. }
  42.  
  43. public static void closeAll(ResultSet rs, Statement st, Connection con)
  44. throws SQLException {
  45. if (rs != null)
  46. rs.close();
  47. if (st != null)
  48. st.close();
  49. if (con != null)
  50. con.close();
  51.  
  52. }
  53.  
  54. }

JDBC.properties

  1. DRIVER=com.mysql.jdbc.Driver
  2. URL=jdbc:mysql:///day06
  3. UNAME=root
  4. PWD=123456

View.java

  1. package com.supergroup.view;
  2.  
  3. import java.sql.SQLException;
  4. import java.util.Scanner;
  5.  
  6. import com.supergroup.domian.User;
  7. import com.supergroup.operation.Operation;
  8.  
  9. public class View {
  10.  
  11. public static void main(String[] args) throws ClassNotFoundException,
  12. SQLException {
  13. // TODO Auto-generated method stub
  14. Scanner sc = new Scanner(System.in);
  15. System.out.println("请输入用户名");
  16. String uname = sc.nextLine();
  17. System.out.println("请输入密码");
  18. String pwd = sc.nextLine();
  19. User user = new User(null, uname, pwd, null);
  20. user = Operation.select(user);
  21. if (user == null) {
  22. System.err.println("登录失败");
  23.  
  24. } else {
  25. System.out.println("登录成功");
  26. System.out.println(user);
  27. }
  28.  
  29. }
  30.  
  31. }
  32. +

day05 java JDBC案例—Android小白的学习笔记的更多相关文章

  1. day04关于MySqL—Android小白的学习笔记

    Mysql入门 1. 数据库基本知识(了解) 1.1.数据库介绍 1.1.1.什么是数据库?数据库的作用是什么? 数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储起来,用户 ...

  2. 20145213《Java程序设计》第八周学习笔记

    20145213<Java程序设计>第八周学习笔记 教材学习内容总结 "桃花春欲尽,谷雨夜来收"谷雨节气的到来意味着寒潮天气的基本结束,气温回升加快.刚出冬的我对于这种 ...

  3. Android安装器学习笔记(一)

    Android安装器学习笔记(一) 一.Android应用的四种安装方式: 1.通过系统应用PackageInstaller.apk进行安装,安装过程中会让用户确认 2.系统程序安装:在开机的时候自动 ...

  4. Java架构师-十项全能学习笔记(1)

    Java架构师-十项全能学习笔记(1) @Configuration @EnableStateMachine public class OrderStateMachineConfig extends ...

  5. android cocos2d-x for Android安装和学习笔记(请用adt-bundle21.1或以上导入)

    引用:http://weimingtom.iteye.com/blog/1483566 (20121108)注意:这篇文章用cdt编译ndk工程的内容已过时(现在可以用adt-bundle,避免配置繁 ...

  6. Java架构师之路 Spring学习笔记(一) Spring介绍

    前言 这是一篇原创的Spring学习笔记.主要记录我学习Spring4.0的过程.本人有四年的Java Web开发经验,最近在面试中遇到面试官总会问一些简单但我不会的Java问题,让我觉得有必要重新审 ...

  7. Android应用开发学习笔记之多线程与Handler消息处理机制

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 和JAVA一样,Android下我们可以通过创建一个Thread对象实现多线程.Thread类有多个构造函数,一般通 ...

  8. Android Room框架学习笔记

    一.使用 1.build.gradle引入 compile "android.arch.persistence.room:runtime:1.0.0" annotationProc ...

  9. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings

    Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation fo ...

随机推荐

  1. js中setTimeout()时间参数设置为0的探讨

    起因源于一道前端笔试题: var fuc = [1,2,3]; for(var i in fuc){ setTimeout(function(){console.log(fuc[i])},0); co ...

  2. php_html转译符号

    1.双引号 /" 或者 " 2.单引号 ' > 4. & &

  3. 全面分析Java的垃圾回收机制

    Java的堆是一个运行时数据区,类的实例(对象)从中分配空间.Java虚拟机(JVM)的堆中储存着正在运行的应用程序所建立的所有对象,这些对象通过new.newarray.anewarray和mult ...

  4. bzoj 3211: 花神游历各国

    #include<cstdio> #include<cmath> #include<iostream> #define M 100006 using namespa ...

  5. Listener实现单态登陆

    MyEclipse中新建Web Project项目,完整目录如下: 需要的jar包为commons-logging-xxx.jar 1.singleton.jsp <%@ page langua ...

  6. collectionview cell吸顶效果

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Hiragino Sans GB"; color: #cf8724 } ...

  7. 第三篇:Retrofit SDK的设计思路

    2016-05-08 15:24:03 Retreofit毫无疑问是一个优美的开源框架,有轻量级.耦合性低.扩展性好.灵活性高的特点,那么Retrofit的设计者们到底是怎么样做到这些的呢?我希望能够 ...

  8. C#中Thread与ThreadPool的比较

    最近同事在编写一个基于UPD RTP协议的通信软件,在处理接收Listen时,发现了一个问题到底是用Thread还是ThreadPool呢? 我看同事的问题比较有典型性,还是做以整理培训一下吧 Thr ...

  9. CentOS安装crontab及使用方法(汇总多人博客并实践确认无误)

    安装centOS: yum -y install vixie-cron --该软件包是cron的主程序 yum -y install crontabs--该软件包用来安装.卸载或者列举需要cron来守 ...

  10. jQuery--事件总结

    标准的绑定: bind(type,[,data],fn)==>第一个参数是事件类型 第二个可选参数作为event.data 传递给事件对象的额外数据对象 第三个参数为用来绑定的处理函数 简写绑定 ...