首页 > 代码库 > 面向对象chapter12

面向对象chapter12

 使用jdbc步骤
  a.导入数据库厂商提供的驱动程序(导入jar包)
  b.加载驱动程序
  Class.forName("驱动程序类");
  c.获得连接
  Connection conn =DriverManager.getConnection(url,username,password);
  url = "jdbc:数据库://ip地址:端口;数据库";
  username="用户名";
  password="密码";
  d.获得执行sql对象,执行sql返回结果
  Statement stat = conn.createStatement();
  ResultSet rs = stat.executeQuery("sql语句");
  e.处理结果集
  while(rs.next()){
  Xxxx s = rs.getXxxx("字段名|数字");
  }
  注意数据下标从1开始
  f.释放资源
  rs.close();
  stat.close();
  conn.close();

 

public class Test1 {
    private static Logger logger = Logger.getLogger(Test1.class.getName());
    public static void main(String[] args) {
        Connection conn = null;
        //1.加载驱动
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            logger.error(e);
        }
        //2.建立连接
        try{
        conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=epet","sa","sa");
        System.out.println("建立连接成功");
        }catch(SQLException e){
            logger.error(e);
        }finally{
            //3.关闭连接
            try{
                if(null!= conn){
                    conn.close();
                    System.out.println("关闭连接成功");
                }
            }catch(SQLException e){
                    logger.error(e);
        }
        
    }
    
    }
}

 

public class Test2 {

    public static void main(String[] args) {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url = "jdbc:sqlserver://localhost:1433;databaseName = schoolDB";
            String username = "sa";
            String password = "sa";
            int deptno = 200;
            String dname = "ww";
            String loc = "dd";
            Connection conn =DriverManager.getConnection(url,username,password);
            Statement stat = conn.createStatement();
//增加        String sql = "insert into DEPT(DEPTNO,dname,LOC)values(800,‘szb‘,‘yiyang‘)";
//修改        String sql1 = "update DEPT set dname =‘szb7‘ where DEPTNO = 800";
//删除        String sql2="delete from dept where deptno=800";    
            String sql3= "insert into DEPT(DEPTNO,dname,LOC)values(?,?,?)";
            PreparedStatement pst = conn.prepareStatement(sql3);
            pst.setInt(1, deptno);
            pst.setString(2, dname);
            pst.setString(3, loc);
            pst.executeUpdate();
//            stat.executeUpdate(sql3);
            stat.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(SQLException e){
            e.printStackTrace();
        }
        
    }

}

 

 

Dao层步骤
        第一步:建表
        第二步:实体类
        第三步:写接口(需求)
        第四步:帮助类(BaseDao)(只需写一次)
        第五步:实现类

        表--->实体类--->dao--->biz--->controller

        serializable 序列化接口
              序列化         接口+实现
                   接口+实现

         2.MVC思想
        1.model:模型层
        2.view:视图层
        3.controller:控制层
        4.业务层:biz,server
          dao层
       
        分包:
            Dao
            Biz
            Controller
            view  (Test)
            entity
            utils

面向对象chapter12