zoukankan      html  css  js  c++  java
  • JDBC再学习

    核心代码:链接

    范例:

    package com.jdbc;
    
    import java.sql.*;
    
    public class Jdbc1 {
        public static void main(String[] args) {
            Connection conn = null;
            PreparedStatement ps = null;
            Statement stmt = null;
            ResultSet rs = null;
            try {// 加载驱动
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                System.out.println("建立连接失败");
            }
    
            try {// 创建连接对象
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hello", "root", "123456");
                stmt = conn.createStatement();// 创建sql操作对象
                String name = "佩奇";
                int health = 40;
                int love = 90;
                String strain = "哈士奇";
    
                StringBuffer sbSql = new StringBuffer("insert into dog (name,health,love,strain) values ( '");
                sbSql.append(name + "',");
                sbSql.append(health + ",");
                sbSql.append(love + ",'");
                sbSql.append(strain + "')");
                // 打印拼接后的sql
                System.out.println(sbSql.toString());
    
                stmt.execute(sbSql.toString());
                // System.out.println("插入成功");
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("建立连接失败");
            } finally {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
    
            }
    
        }
    }
    View Code

      

     java database connectivity

    回顾:

    第一个标准代码:

    package com.test;
    //先在mysql里面执行
    import java.sql.*;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    
    import com.mysql.jdbc.Statement;
    
    public class TestJdbc {
    public static void main(String[] args) {
        Connection ct=null;
        Statement st=null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.得到连接
            ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/hello","root","123456");
            //3.创建sql对象
            st=(Statement) ct.createStatement();
            //通过Statement发sql指令
        //通过Statement向数据库发送指令
            //n代表影响了几条
            int n=st.executeUpdate("insert into user values(1,'张三','zhangsan','zhangsan@163.com','1997-12-24',12)");//update insert delete
            
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(st!=null){
                try{
                    st.close();
            }catch(Exception e){
                e.printStackTrace();
            }
                st=null;
            if(ct!=null){
                try{
                    ct.close();        
    
            }catch(Exception e){
                e.printStackTrace();
            }
                ct=null;
        }
            }}//finally
        
    }
    }
    View Code

    DriverManager用于加载驱动

    Connection接口代表数据库的连接,客户端与数据库所有的交互

    抛出异常的代码:

    package com.test;
    //先在mysql里面执行
    import java.sql.*;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    
    import com.mysql.jdbc.Statement;
    
    public class TestJdbc {
    public static void main(String[] args) {
        Connection ct=null;
        Statement st=null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.得到连接
            ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/hello","root","123456");
            //3.创建sql对象
            st=(Statement) ct.createStatement();
            //通过Statement发sql指令
        //通过Statement向数据库发送指令
            //n代表影响了几条
            int n=st.executeUpdate("insert into user values(2,'张三','zhangsan','zhangsan@163.com','1997-12-24',12)");//update insert delete
            int t=1/0;
            st.executeUpdate("update user set age=8 where id=1;");
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(st!=null){
                try{
                    st.close();
            }catch(Exception e){
                e.printStackTrace();
            }
                st=null;
            if(ct!=null){
                try{
                    ct.close();        
    
            }catch(Exception e){
                e.printStackTrace();
            }
                ct=null;
        }
            }}//finally
        
    }
    }
    View Code

    解决方法:将事务设置成不自动提交(未解决)

     Connection ct=null;

    输出连接ct 接口类的类型

     RsultSet表示sql语句执行的结果:

    指向结果的前一行

    st.executeQuery(sql) 查询功能

    st.executeUpdate(sql) update insert delete功能

    实现简单的查询功能

    package com.test;
    //先在mysql里面执行 为啥回滚不行
    import java.sql.*;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    
    import com.mysql.jdbc.Statement;
    
    public class Jdbc1 {
    public static void main(String[] args) {
        Connection ct=null;
        Statement st=null;
        ResultSet rs=null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.得到连接
            ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/hello","root","123456");
            //把事务设置成不自动提交
            ct.setAutoCommit(false);
            System.out.println(ct);
            //3.创建sql对象
            st=(Statement) ct.createStatement();
            rs=st.executeQuery("select*from user");
            while(rs.next()){
                System.out.println(rs.getInt(1)+" "+rs.getString(2));
            }
    
        } catch (Exception e) {
            // TODO Auto-generated catch block
            //sql语句任何一行语句出错,可以整体回滚
            e.printStackTrace();
        }finally{
            if(st!=null){
                try{
                    st.close();
            }catch(Exception e){
                e.printStackTrace();
            }
                st=null;
            if(ct!=null){
                try{
                    ct.close();        
    
            }catch(Exception e){
                e.printStackTrace();
            }
                ct=null;
        }
            }}//finally
    }
    }
    View Code

    ResultSet详解:

    复用:rs.beforeFirst();

    package com.test;
    //先在mysql里面执行 为啥回滚不行
    import java.sql.*;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    
    import com.mysql.jdbc.Statement;
    
    public class Jdbc1 {
    public static void main(String[] args) {
        Connection ct=null;
        Statement st=null;
        ResultSet rs=null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.得到连接
            ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/hello","root","123456");
            //把事务设置成不自动提交
            ct.setAutoCommit(false);
            System.out.println(ct);
            //3.创建sql对象
            st=(Statement) ct.createStatement();
            rs=st.executeQuery("select*from user");
            while(rs.next()){
                System.out.println(rs.getInt(1)+" "+rs.getString(2));
            }
            rs.beforeFirst();
            System.out.println("复用");
            while(rs.next()){
                System.out.println(rs.getInt(1)+" "+rs.getString(2));
            }
    
        } catch (Exception e) {
            // TODO Auto-generated catch block
            //sql语句任何一行语句出错,可以整体回滚
            e.printStackTrace();
        }finally{
            if(st!=null){
                try{
                    st.close();
            }catch(Exception e){
                e.printStackTrace();
            }
                st=null;
            if(ct!=null){
                try{
                    ct.close();        
    
            }catch(Exception e){
                e.printStackTrace();
            }
                ct=null;
        }
            }}//finally
    }
    }
    View Code

    SqlHelper的书写:

    1.访问数据库很频繁,Connection的话不要设置为static
    2.

  • 相关阅读:
    【BZOJ1023】仙人掌图(SHOI2008)-圆方树+DP+单调队列
    【BZOJ4816】数字表格(SDOI2017)-莫比乌斯反演+数论分块
    【BZOJ3529】数表(SDOI2014)-莫比乌斯反演+树状数组
    【BZOJ3714】Kuglarz(PA2014)-最小生成树
    javascript div元素后追加节点
    php多文本框提交
    有几数组表单,js怎么获得数组并动态相加输出到文本框
    SqlCommand.Parameters.add()方法
    ASP.net后台弹出消息对话框的方法!【转】
    Access中的SELECT @@IDENTITY
  • 原文地址:https://www.cnblogs.com/helloworld2019/p/11013262.html
Copyright © 2011-2022 走看看