jdbc

代码块
```package com.foreknow.jdbcdemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * JDBC:连接数据库的一种技术 /持久层(DAO)的技术
 * JDBC的API文件:java.sql包
 * JDBC访问数据库的步骤
 * 1.加载驱动程序  每个数据库厂商都会给我们提供它自己的驱动程序   Class.forName("驱动程序的名称")
 * 2.获取连接(Connection接口)
 * 3.我们会通过连接(Connection接口)中提供好的方法创建一个对象 Statement(接口),用于将SQL语句发送给数据库
 * ResultSet executeQuery(String sql) 
 * 执行给定的 SQL 语句,该语句返回单个 ResultSet (结果集)对象(用于保存查询的结果)。 
 *  int executeUpdate(String sql) 
     执行给定 SQL 语句,该语句可能为 INSERT、UPDATE 或 DELETE 语句,或者不返回任何内容的 SQL 语句(如 SQL DDL 语句)。
     (1) 对于 SQL 数据操作语言 (DML) 语句,返回行计数   delete insert update 成功返回值就是1
     (2) 对于什么都不返回的 SQL 语句,返回 0 
 * 4.释放资源(关闭资源)
 * Statement   close()          Connection  close()
 * @author ttc
 *
 */
public class TestJdbc {
    Connection conn;
    Statement statement;
    ResultSet rs;
    //连数据库的方法
    public Connection Link() throws ClassNotFoundException, SQLException{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysqldemo?useUnicode=true&characterEncoding=utf8&useSSL=true", "root", "123456");
           return conn;
    }
    //查询方法
    public ResultSet find(String sql) throws SQLException{
         statement=conn.createStatement();
        rs=statement.executeQuery(sql);
        return rs;
    }
    //DML的方法
    public int Update(String sql) throws SQLException{
        statement=conn.createStatement();
        int isRight=statement.executeUpdate(sql);
        return isRight;
    }
    //关闭资源
    public void closed() throws SQLException{
        if (rs!=null) {
            rs.close();
        }
        if(conn!=null){
        conn.close();
        }
    }
    public static void main(String[] args) {    
        try {
            //1.加载驱动  https://mvnrepository.com
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接(Connection接口)
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysqldemo?useUnicode=true&characterEncoding=utf8&useSSL=true", "root", "123456");
            //jdbc中默认的食物提交方式为自动提交(不用自己commit)
            //为TRUE表示自动提交模式;为FALSE表示禁用自动提交模式
            conn.setAutoCommit(false);
            //3.获取Statement对象(接口)
            Statement statement=conn.createStatement();
            int isRight=statement.executeUpdate("insert into dept values(50,'aaa','bbb')");
            //提交事物(如果不提交事物,数据库中不会更新)
            conn.commit();
            System.out.println(isRight);
//          ResultSet rs=statement.executeQuery("select * from emp");
//          //4.如何让获取到结果集中的数据
//          //boolean next()将光标从当前位置线下移
//          List<Emp> list=new ArrayList<>();
//          while (rs.next()) {
//              //获取当前行的每一列的数据
//              int empno=rs.getInt("empno");
//              String ename=rs.getString("ename");
//              String mgr=rs.getString("mgr");
//              System.out.println(empno+"---"+ename+"---"+mgr);
//              //见获取到的数据以EMP对象的方式保存到LIST集合里
//              Emp emp=new Emp();
//              emp.setEmptno(empno);
//              emp.setEname(ename);
//              emp.setMgr(mgr);
//              list.add(emp);
//          }
//          for (Emp emp : list) {
//              System.out.println(emp.getEmptno()+"  "+emp.getEname()+" "+emp.getMgr());
//          }
            //5.关闭资源
//          rs.close();
            statement.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    

}


Test代码块


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        TestJdbc t=new TestJdbc();
            try {
                t.Link();
//              ResultSet rs=t.find("select * from emp");
//              List<Emp> list=new ArrayList<>();
//              while (rs.next()) {
//                  //获取当前行的每一列的数据
//                  int empno=rs.getInt("empno");
//                  String ename=rs.getString("ename");
//                  String mgr=rs.getString("mgr");
//                  System.out.println(empno+"---"+ename+"---"+mgr);
//                  //见获取到的数据以EMP对象的方式保存到LIST集合里
//                  Emp emp=new Emp();
//                  emp.setEmptno(empno);
//                  emp.setEname(ename);
//                  emp.setMgr(mgr);
//                  list.add(emp);
//              }
//              for (Emp emp : list) {
//                  System.out.println(emp.getEmptno()+"  "+emp.getEname()+" "+emp.getMgr());
//              }
                
             int is=t.Update("insert into dept values(57,'aaa','bbb')");
             System.out.println(is);
             t.closed();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        
    }

}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容