JDBC个人笔记
来源:互联网 发布:怎样联系网络推手 编辑:程序博客网 时间:2024/06/18 16:08
JDBC工具封装
import java.sql.DriverManager;import java.sql.Connection;public class JdbcUtil { static { try{ // 返回与带有给定字符串名的类或接口相关联的 Class对象。即返回驱动路径 Class.forName("com.mysql.jdbc.Driver.class"); }catch(Exception e){ e.printStackTrace(); } } public Connection getConnection(){ //地址 127.0.0.1是本机地址,端口3306 String url="jdbc:mysql://127.0.0.1:3306/test"; //mysql用户名 String user ="hui"; //mysql用户的密码 String password ="password~!"; //新建一个连接通道 Connection con=null; try{ //通过地址 用户名 密码 登录mysql并连接 con=DriverManager.getConnection(url, user, password); }catch(Exception e){ e.printStackTrace(); } return con; }}
测试代码
(在这里只测试一下查询和插入)
import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class JdbcTest{ public void getfind(){ JdbcUtil jdbc =new JdbcUtil(); Connection con=null; String sql=null; Statement st=null; ResultSet result=null; try{ con=jdbc.getConnection(); sql = "select *from student"; st=con.createStatement(); result = st.executeQuery(sql); //result.first(); while(result.next()){ String name=result.getString("name"); String no = result.getString("no"); String point =result.getString("point"); System.out.println(name+no+point); } }catch(Exception e){ e.printStackTrace(); }finally{ } } public void insertTest(){ JdbcUtil jdbc=new JdbcUtil (); Connection con=null; Statement st=null; String sql=null; ResultSet rs=null; try { con=jdbc.getConnection(); sql="insert into student values('姜木','0004','59')"; st=con.createStatement(); int i =st.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { //插入 new JdbcTest().insertTest(); //查询 new JdbcTest().getfind(); }}
未知参数查询演示
public class JdbcArg { public void Query(String no ){ JdbcUtil jdbc=new JdbcUtil (); Connection con =null; PreparedStatement pst=null; ResultSet rs=null; String sql; try{ con =jdbc.getConnection(); sql = "select * from student where no =?"; pst =con .prepareStatement(sql); pst.setString(1, no); rs=pst.executeQuery(); while (rs.next()){ System.out.println(no + rs.getString("name")+rs.getString("point")); } }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(rs!=null){ rs.close(); } if(pst!=null){ pst.close(); } if(con!=null){ con.close(); } }catch(Exception e){ e.printStackTrace(); } } } public static void main(String[] args) { new JdbcArg().Query("0001"); }}
调用函数和过程的时候的测试
public class JdbcCall { public void JdbcFunctionSys(){ JdbcPool jdbc = new JdbcPool(); Connection con = null; String sql; CallableStatement cst = null; ResultSet res = null; try { //建立连接 con = jdbc.getConnection(); //执行的SQL sql = "select CONCAT('ma','shen','java5')"; //把SQL调用放到连接的prepareCall方法里,得到一个用于处理函数或者过程的对象CallableStatement cst = con.prepareCall(sql); //CallableStatement对象执行查询并得到结果,使用resultset来存储 res = cst.executeQuery(); //执行结果集的读取一行操作 res.next(); System.out.println(res.getString(1)); } catch (Exception e) { e.printStackTrace(); }finally { try { if (cst!=null) cst.close(); if (res!=null) res.close(); if (con!=null) con.close(); } catch (Exception e2) { e2.printStackTrace(); } } } public void JdbcFunctionSysOpt(String str1,String str2,String str3){ JdbcPool jdbc = new JdbcPool(); Connection con = null; String sql; CallableStatement cst = null; ResultSet res = null; try { //建立连接 con = jdbc.getConnection(); //执行的SQL sql = "select CONCAT(?,?,?)"; //把SQL调用放到连接的prepareCall方法里,得到一个用于处理函数或者过程的对象CallableStatement cst = con.prepareCall(sql); //设置SQL里要传入的值 cst.setString(1, str1); //设置SQL里要传入的值 cst.setString(2, str2); //设置SQL里要传入的值 cst.setString(3, str3); //执行SQL查询,得到resultset结果集 res = cst.executeQuery(); //执行结果集的读取一行操作 res.next(); System.out.println(res.getString(1)); } catch (Exception e) { e.printStackTrace(); }finally { try { if (cst!=null) cst.close(); if (res!=null) res.close(); if (con!=null) con.close(); } catch (Exception e2) { e2.printStackTrace(); } } } public void JdbcFunctionSysOptArg(int i1,int i2){ JdbcPool jdbc = new JdbcPool(); Connection con = null; String sql; CallableStatement cst = null; ResultSet res = null; try { //建立连接 con = jdbc.getConnection(); //执行的SQL sql = "select xiemn_mul(?,?)"; //把SQL调用放到连接的prepareCall方法里,得到一个用于处理函数或者过程的对象CallableStatement cst = con.prepareCall(sql); //设置SQL里要传入的值 cst.setInt(1, i1); //设置SQL里要传入的值 cst.setInt(2, i2); //执行SQL查询,得到resultset结果集 res = cst.executeQuery(); //结果集读取一行操作 res.next(); System.out.println(res.getInt(1)); } catch (Exception e) { e.printStackTrace(); }finally { try { if (cst!=null) cst.close(); if (res!=null) res.close(); if (con!=null) con.close(); } catch (Exception e2) { e2.printStackTrace(); } } } public void JdbcProcedureSysOptArg(int i1,int i2){ JdbcPool jdbc = new JdbcPool(); Connection con = null; String sql; CallableStatement cst = null; ResultSet res = null; try { //建立连接 con = jdbc.getConnection(); //要执行SQL sql = "{call xiemnp_mul(?,?,?)}"; //把SQL调用放到连接的prepareCall方法里,得到一个用于处理函数或者过程的对象CallableStatement cst = con.prepareCall(sql); //设置SQL里要传入的值 cst.setInt(1, i1); //设置SQL里要传入的值 cst.setInt(2, i2); //把过程中的输出值类型进行注册 cst.registerOutParameter(3, java.sql.Types.INTEGER); //执行过程 cst.execute(); //获取注册后参数的值 System.out.println(cst.getInt(3)); } catch (Exception e) { e.printStackTrace(); }finally { try { if (cst!=null) cst.close(); if (res!=null) res.close(); if (con!=null) con.close(); } catch (Exception e2) { e2.printStackTrace(); } } } public static void main(String[] args) {// new JdbcCall().JdbcFunctionSys();// new JdbcCall().JdbcFunctionSysOpt("ma", "shen", "java5");// new JdbcCall().JdbcFunctionSysOptArg(2342, 23432); new JdbcCall().JdbcProcedureSysOptArg(2342, 23432); }}
1 0
- JDBC个人笔记
- 个人JDBC学习笔记(未完待续)
- 数据库与JDBC(个人学习笔记,不断更新修改中)
- 【JDBC笔记】JDBC基础知识
- 个人笔记
- 个人笔记
- 个人笔记
- 个人笔记
- 个人笔记
- 个人笔记
- 个人笔记
- 个人笔记
- 个人笔记
- 个人笔记
- 个人笔记
- 个人笔记
- 个人笔记
- 个人笔记
- AndroidStudio快捷键设置
- thinkphp的dump函数无输出
- 买饮料难题
- 几种触发点击事件的方法
- 本月有几天
- JDBC个人笔记
- keytool生成私钥、公钥、证书详解
- 如何把同一个Android项目同时安装到手机上--Android系统如何区分不同app
- dojo 特性
- sap 四种数据共享方式
- [HDU5788] Level Up [2016 Multi-University Training Contest 5 1008 (2016多校联合训练5)]
- 谈谈对面向对象编程的认识
- HDU 1564 Play a game
- Tomcat与IIS共用80端口