• JAVA-jdbc-preparestatement接口-预编译sql


    package cn.bruce.MySql;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    public class Prepared {
        public static void main(String[] args) {
            String name, password, url, sql, jdbc;
            Connection con = null;
            name = "root";
            password = "jinqi1986";
            url = "jdbc:mysql://localhost:3306/mytrain?useSSL=false&serverTimezone=CTT";
            jdbc = "com.mysql.cj.jdbc.Driver";
            try
            {
                Class.forName(jdbc);
                System.out.println("驱动加载成功");
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            try
            {
                con = DriverManager.getConnection(url, name, password);
                System.out.println("连接成功");
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            try
            {
                String n = "小金11";
                String p = "12345622";
                sql = "update  users set zusename = ?, zpassword = ? where id = ?;";
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setString(1, n);
                ps.setString(2, p);
                ps.setInt(3, 2);
                System.out.println(ps.executeUpdate() + "行进行修改");
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            try
            {
                String n = "小金22";
                String p = "334455";
                String n1 = "小金33";
                String p1 = "22334455";
                sql = "insert into  users (zusename,zpassword) values (?,?),(?,?);";
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setString(1, n);
                ps.setString(2, p);
                ps.setString(3, n1);
                ps.setString(4, p1);
                System.out.println(ps.executeUpdate() + "行进行修改");
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            try
            {
                sql = "select * from users;";
                PreparedStatement ps = con.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next())
                {
                    System.out.println(
                            rs.getString("id") + "  " + rs.getString("zusename") + "  " + rs.getString("zpassword"));
                }
                ps.close();
            } catch (Exception e)
            {
                e.printStackTrace();
            } finally
            {
                try
                {
                    con.close();
    
                } catch (Exception e2)
                {
                    e2.printStackTrace();
                }
            }
        }
    }

  • 相关阅读:
    WDK中出现的特殊代码
    敏捷是怎样炼成的
    推荐一个非常好玩的falsh游戏
    软件安全技术
    J2EE的昨天,今天,明天
    Java打印程序设计
    关于父亲
    xml发展历史和用途
    CRM与ERP整合的六个切入点
    SEO(搜索引擎最佳化)简介
  • 原文地址:https://www.cnblogs.com/BruceKing/p/13743927.html
Copyright © 2020-2023  润新知