JDBC : (JavaDatabase Connectivity:java数据库连接) :
什么是 JDBC ?
SUN公司为了简化开发人员(对数据库统一)的操作,提供了一个 java 操作数据库的 规范 ,这就是 JDBC 。
或者说, JDBC 是一款实现不同数据语言驱动的 接口 。
JDBC 的操作步骤:
注册驱动--→2. 登录数据库--→3. 创建执行 SQL语句 对象--→4. 执行 SQL语句 --→5. 处理查询结果集(如果 SQL语句 不是 select语句 ,那不需要这一步)--→6. 释放资源package JDBC; import java.sql.*; import java.util.ResourceBundle; public class jdbcUse { public static void main(String[] args) { //1. 使用资源绑定器绑定属性配置文件(配置文件包括注册,登录) ResourceBundle jdbc = ResourceBundle.getBundle("JDBCbd"); String Driver = jdbc.getString("Driver"); String url = jdbc.getString("url"); String user = jdbc.getString("user"); String password = jdbc.getString("password"); //2. 定义连接对象初始值为null Connection connection = null; //3. 定义操作数据库语言对象初始值为null Statement statement = null; try { //4. 利用配置文件数据进行--注册,登录 Class.forName(Driver); connection = DriverManager. getConnection(url,user,password); //5. 获取数据库操作对象 statement = connection.createStatement(); //6. 执行SQL语句 String sql = "insert into dept(deptno,dname,loc) values(40,‘SMITHS‘,‘KEORA‘)"; int i = statement.executeUpdate(sql); System.out.println(i == 1 ? "true" : "false"); //6.5 获取查询结果集 // 7. 释放资源 } catch (Exception e) { e.printStackTrace(); } finally { if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }下面是利用 JDBC 功能实现用户的模拟登录案例;
package Case; import java.sql.*; import java.util.*; //总结:老师在课上讲的步骤要清楚,编程思维:我理解的是,遇到编程方面问题,第一时间会想到相对固定的解决方案,即;泛型的使用,hashmap的使用,添加变量的使用等等。 //唯手熟尔,多转换思维把,加油练! public class aaa { public static void main(String[] args) { //要干嘛? 模拟使用数据库中数据进行登录操作; //登录需要什么? 账号密码呀! //用什么存储账号密码最方便? HashMap呀 //HashMap怎么用? 明天学 //操作步骤 //1. 获取一个存放账号密码的HashMao Map<String, String> userLoginInfo = initUI(); //2. 判断这个存储账号密码的HashMap是否正确 boolean loginSuccess = login(userLoginInfo); //3. 正确返回什么,错误返回什么 System.out.println(loginSuccess ? "登录成功" : "登录失败"); } /** * 验证用户名和密码 * @return boolean * @params userLoginInfo */ private static boolean login(Map<String, String> uIL) { boolean loginSuccess = false; Connection connection = null; Statement statement = null; ResultSet resultSet = null; ResourceBundle bundle = ResourceBundle.getBundle("case_userLogin"); String getName = uIL.get("userName"); String getPwd = uIL.get("password"); String driver = bundle.getString("Driver"); String url = bundle.getString("url"); String user = bundle.getString("user"); String password = bundle.getString("password"); try { Class.forName(driver); connection = DriverManager.getConnection(url, user, password); statement = connection.createStatement(); String sql = "select * from t_user where loginName = ‘"+ getName+"‘ and loginPwd = ‘"+getPwd+"‘"; resultSet = statement.executeQuery(sql); //resultSet.next()可以判断是否有数据 if (resultSet.next()) loginSuccess = true; } catch (Exception e) { e.printStackTrace(); } finally { if (resultSet != null) try { resultSet.close(); } catch (Exception e) { e.printStackTrace(); } if (statement != null) try { statement.close(); } catch (Exception e) { e.printStackTrace(); } if (connection != null) try { connection.close(); } catch (Exception e) { e.printStackTrace(); } } return loginSuccess; } /** * 初始化用户界面 * * @return 返回用户用户名和密码信息 * @params 没有参数 */ private static Map<String, String> initUI() { Scanner s = new Scanner(System.in); System.out.println("用户名:"); String userName = s.nextLine();//一次读一行; System.out.println("密码:"); String password = s.nextLine(); Map<String, String> userloginInfo = new HashMap<>(); userloginInfo.put("userName", userName); userloginInfo.put("password", password); return userloginInfo; } }
JDBC:(JavaDatabase Connectivity:java数据库连接):
标签:stat ams 参数 rom mit 初始 aaa init 用户名
查看更多关于JDBC:(JavaDatabase Connectivity:java数据库连接):的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did117031