SQL---JDBC基础6步
生活随笔
收集整理的這篇文章主要介紹了
SQL---JDBC基础6步
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Connection conn = null;Statement sta = null;ResultSet rs = null;try {//1、注冊驅(qū)動Class.forName("com.mysql.jdbc.Driver");//2、獲取連接conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","(密碼)");//3、獲取數(shù)據(jù)庫連接對象sta = conn.createStatement();//4、執(zhí)行SQLString sql = "select ename,sal from emp;";rs = sta.executeQuery(sql);//5.處理查詢結(jié)果集while(rs.next()){String ename = rs.getString("ename");String sal = rs.getString("sal");System.out.println("名稱:"+ename+",工資:"+sal);}} catch (Exception e) {e.printStackTrace();}finally{//釋放資源if (rs != null) {try {rs.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (sta != null) {try {sta.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException throwables) {throwables.printStackTrace();}}}
總結(jié)
以上是生活随笔為你收集整理的SQL---JDBC基础6步的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 创建一个类 new 与 不加new 有什
- 下一篇: 字符串工具类---StringUtils