zoukankan      html  css  js  c++  java
  • JDBC

    1使用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();
            }
            
        }
    
    }
    
    
    
     
  • 相关阅读:
    实践详细篇-Windows下使用Caffe训练自己的Caffemodel数据集并进行图像分类
    实践详细篇-Windows下使用VS2015编译的Caffe训练mnist数据集
    实践详细篇-Windows下使用VS2015编译安装Caffe环境(CPU ONLY)
    Linux上rpm实战搭建FTP服务器
    Idea打包Maven项目
    修改Idea默认的全局设置,如Maven等
    IntelliJ IDEA 项目结构旁边出现 0%classes,0% lines covered
    Idea中JDK为1.8,还提示Diamond types are not supported at this language level
    如何避免在父类转换到子类时发生java.lang.ClassCastException
    HashMap常用方法总结
  • 原文地址:https://www.cnblogs.com/yangchan250/p/6821900.html
Copyright © 2011-2022 走看看