zoukankan      html  css  js  c++  java
  • JDBC程序实例

    实例 ( Statement ):

    public class JDBC {
        public static void main(String[] args) throws Exception {
            Connection conn = null;
            String sql;
            // MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称?参数=值
            // 避免中文乱码要指定useUnicode和characterEncoding
            // 执行数据库操作之前要在数据库管理系统上创建一个数据库,名字自己定,
            // 下面语句之前就要先创建javademo数据库
            String url = "jdbc:mysql://192.168.8.83:3306/test?"
                    + "user=root&password=root&useUnicode=true&characterEncoding=UTF8";
            try {
                // 之所以要使用下面这条语句,是因为要使用MySQL的驱动,所以我们要把它驱动起来,
                // 可以通过Class.forName把它加载进去,也可以通过初始化来驱动起来,下面三种形式都可以
                Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动
                // or:
                // com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
                // or:
                // new com.mysql.jdbc.Driver();
                System.out.println("成功加载MySQL驱动程序");
                // 一个Connection代表一个数据库连接
                conn = DriverManager.getConnection(url);
                // Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等
                Statement stmt = conn.createStatement();
                sql = "create table student(NO char(20),name varchar(20),primary key(NO))";
                int result = stmt.executeUpdate(sql);// executeUpdate语句会返回一个受影响的行数,如果返回-1就没有成功
                if (result != -1) {
                    System.out.println("创建数据表成功");
                    sql = "insert into student(NO,name) values('2012001','陶伟基')";
                    result = stmt.executeUpdate(sql);
                    sql = "insert into student(NO,name) values('2012002','周小俊')";
                    result = stmt.executeUpdate(sql);
                    sql = "select * from student";
                    ResultSet rs = stmt.executeQuery(sql);// executeQuery会返回结果的集合,否则返回空值
                    System.out.println("学号	姓名");
                    while (rs.next()) {
                        System.out.println(rs.getString(1) + "	" + rs.getString(2));// 入如果返回的是int类型可以用getInt()
                    }
                }
            } catch (SQLException e) {
                System.out.println("MySQL操作错误");
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                conn.close();
            }
        }
    }

    实例 ( PreparedStatement ):

    public class JDBC {
        public static void main(String[] args) throws Exception {
            PreparedStatement pstmt=null; 
            Connection con = null;
            try {  
                Class.forName("com.mysql.jdbc.Driver");  
                System.out.println("Loading Driver succeeds");  
                String url="jdbc:mysql://192.168.8.83:3306/test";  
                String user="root";  
                String password="root";  
                con =DriverManager.getConnection(url,user,password);  
                //Statement statement=con.createStatement();  
                String sql= "select NO,name,password from student where name like ? or password like ?";//先占座,在等待来  
                  //int num=statement.executeUpdate(sql);  
                pstmt=con.prepareStatement(sql);  
                pstmt.setString(1,"%"+"c"+"%");//前面的int型数字代表,sql语句中?的那一列 。设置sql中?的值,数据库中的下标从0开始  
                pstmt.setString(2, "%"+"6"+"%");  
                ResultSet resultSet=pstmt.executeQuery();//执行查询  
                //System.out.println("&&&");  
                while(resultSet.next()){  
                    System.out.println(resultSet.getInt(1)+" "+resultSet.getString(2)+" "+resultSet.getString(3));//放在ResultSet中的查询结果 下标从1开始  
                }  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  finally {
                if(pstmt != null){
                    pstmt.close(); 
                } 
                if(con != null){
                    con.close();  
                } 
            }
        } 
    }
  • 相关阅读:
    C# 读写 ini 配置文件
    [转]VB 读写ini 配置文件
    js实现隔行变色-------Day40
    Camel Games借助AWS在爆发式增长中提供优质游戏体验
    深入浅出--UNIX多进程编程之fork()函数
    【玩转微信公众平台之八】 演示样例代码分析
    jQuery 选择具有特殊属性的元素
    下载超星或读秀图书时,怎么搞定完整书签?
    意外的php之学习笔记
    POJ 1182 (经典食物链 /并查集扩展)
  • 原文地址:https://www.cnblogs.com/wylblogs/p/JDBC.html
Copyright © 2011-2022 走看看