栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JDBC的一些基础操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JDBC的一些基础操作

JDBC的规范:      Java 出现后,要使用 Java 连接数据库执行 SQL 语句,SUN 有两个选择:使用微软已经 成熟成为标准的 ODBC 规范;发明自己新的规范。不管什么原因,SUN 肯定选择后者。 所以, JDBC 是 SUN 制定的一套用 Java 连接数据库执行 SQL 语句的规范,标准,现在版 本为 4.0。 使用JDBC:    step1:使用 Class.forName()加载驱动程序。(Class.forName(“driverName”)代码完成两个功能:第一,把驱动程序加载到内存里;第二,把当前加载的驱动程序 自动去 DriverManager 那注册,DriverManager 是 JDBC 规 范中唯一的 Java 类)    
try {
        Class.forName("实现java.sql.Driver接口的一个类DriverName");
}
catch (ClassNotFoundException e) {
        System.out.println("驱动程序类没有找到");
        e.printStackTrace();
}
  step2:建立连接( 通过 DriverManager.getConnection()方法得到连接对象。)
try {
String url = "ip+port+dbName";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(url, "sa", "");
System.out.println("驱动程序加载完毕");
}
catch (ClassNotFoundException e) {
System.out.println("驱动程序类没有找到");
e.printStackTrace();
}
catch (SQLException e) {
System.out.println("发生了sql异常");
e.printStackTrace();
}
  step3:创建startment对象
con = DriverManager.getConnection(url, "SA", "SA");
System.out.println(con);
sta = con.createStatement();
  step4:执行SQL语句( 1. executeUpdate()用来执行 insert,update,delete 语句。 2. 返 回的类型为 int,表示 sql 语句影响的行数。如果执行一个 delete 语句删除 3 行,那 sql 语句影响 3 行,返回 3;如果执行一个 update 语句,修改 2 行,那 sql 语句影响 2 行,返回 2;这里执行 insert 语句,新增了 1 行,即 sql 语句影响 1 行,返回 1。 )
 String sql = "insert into teacher(teanum, teaName, teatitle, teatypeid, teabtd, pwd, static) values ('20210001', '玄易子', '高级教师', 1, '1945-5-7', 1, 1)";
int row = sta.executeUpdate(sql);
if (row == 1) {
    System.out.println("op success");
else {
     System.out.println("op error");
}
  step5:关闭资源(Connection 与 Statement 对象设为全局变量,否则在 finally 块中无法拿到其引用。)
Connection con = null;
Statement sta = null;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("驱动程序加载完毕!");
            String url = "jdbc:sqlserver://localhost:1433;databaseName=pas";
            con = DriverManager.getConnection(url, "sa", "sa");
            System.out.println(con);
            sta = con.createStatement();
           String sql = "insert into teacher(teanum, teaName, teatitle, teatypeid, teabtd, pwd, static) values ('20210001', '玄易子', '高级教师', 1, '1945-5-7', 1, 1)";
            int rows = sta.executeUpdate(sql);
            if (rows == 1) {
                System.out.println("op success");
            } else {
                System.out.println("op error");
            }
        } catch (ClassNotFoundException e) {
            System.out.println("驱动程序没有找到");
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(sta != null) {
                    sta.close();
                    sta = null;
                }
                if(con != null) {
                    con.close();
                    con = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        System.out.println("后面代码");
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/292470.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号