2021-07-30JDBC练习

  • 练习:JDBC执行 insert delete update
package com.bipowernode;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Test01 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;

        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","123");
            //3.获取数据库操作对象
            stmt = conn.createStatement();
            //4.执行sql语句
            /*int count  = stmt.executeUpdate("update dept set dname = '销售部',loc = '西安' where deptno = 20;");
            System.out.println(count==1?"修改成功":"修改失败");*/
           /* int count = stmt.executeUpdate("insert into dept(deptno,dname,loc) values (50,'人事部','深圳')");
            System.out.println(count == 1 ? "增加成功":"增加失败");*/
           int count = stmt.executeUpdate("delete from dept where deptno = 50 ");
            System.out.println(count == 1 ? "删除成功":"删除失败");
            //5.查询结果集

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //6.释放资源
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
  • 练习: 用户登录系统
package com.bipowernode;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test02 {
    public static void main(String[] args) {
        Map<String,String> userLoginInfo  =initUI();
        boolean loginSuccess = login(userLoginInfo);
        System.out.println(loginSuccess ? "登陆成功":"登录失败");
    }

    /**
     * 用户登录
     * @param userLoginInfo 用户登录信息
     * @return  false表示失败  true表示成功
     */
    private static boolean login(Map<String, String> userLoginInfo) {
        //打标记
        boolean loginSuccess = false;
        //单独定义变量
        String loginNeme = userLoginInfo.get("loginName");
        String loginPwd = userLoginInfo.get("loginNPwd");

        //JDBC代码
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","123");
            //3.获取数据库操作对象
            stmt = conn.createStatement();
            //4.执行sql语句
            String sql = "select * from t_user where loginName = '"+loginNeme+ "' and loginPwd = '" + loginPwd+ "'";
           rs = stmt.executeQuery(sql);
            //5.查询结果集
            if (rs.next()){
                loginSuccess = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //6.释放资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return loginSuccess;
    }


    /**
     * 初始化用户界面
     * @return 用户输入的用户名和密码等信息
     */
    private static Map<String, String> initUI() {
        Scanner s = new Scanner(System.in);

        System.out.print("请输入用户名:");
        String loginName = s.nextLine();

        System.out.print("请输入密码:");
        String loginPwd = s.nextLine();

        Map<String,String> userLoginInfo = new HashMap<>();
        userLoginInfo.put("loginName",loginName);
        userLoginInfo.put("loginNPwd",loginPwd);

        return userLoginInfo;
    }

}
  • 练习: 处理查询结果集
package com.bipowernode;

import java.sql.*;

public class Test04 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.建立连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","123");
            //3.获取数据库操作对象
            stmt = conn.createStatement();
            //4.执行sql语句
            rs = stmt.executeQuery("select ename,empno,sal from emp ");
            //5.获取查询结果集
            while (rs.next()){
               /* String empno = rs.getString(1);
                String ename = rs.getString(2);
                String sal = rs.getString(3);
                System.out.println(empno+","+ename+","+sal);*/

                /*String empno = rs.getString("empno");
                String ename = rs.getString("ename");
                String sal = rs.getString("sal");
                System.out.println(empno+","+ename+","+sal);*/

                /*int empno = rs.getInt(2);
                String ename = rs.getString(1);
                double sal = rs.getDouble(3);
                System.out.println(empno+","+ename+","+sal);*/

                int empno = rs.getInt("empno");
                String ename = rs.getString("ename");
                double sal = rs.getDouble("sal");
                System.out.println(empno+","+ename+","+sal);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {        //6.资源释放
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            } if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            } if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
}
  • 练习: 使用prepareStatement
package com.bipowernode;
import java.sql.*;
public class Test05 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            //1.创建驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","123");
            //3.获取数据库操作对象
            String sql = "select empno,ename,sal from emp where deptno = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,"10");
            //4.执行sql语句
            rs = ps.executeQuery();
            //5.处理查询结果集
            while (rs.next()){
                int empno = rs.getInt(1);
                String ename = rs.getString(2);
                double sal = rs.getDouble(3);
                System.out.println(empno+","+ename+","+sal);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //6.释放资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

            }
}
  • 练习:降序或者升序
package com.bipowernode;

import java.sql.*;
import java.util.Scanner;

public class Test06 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("请输入desc或acs:");
        String KeyWords = s.nextLine();


        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            //1.创建驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","123");
            //3.获取数据库操作对象
            String sql = "select ename from emp order by ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,KeyWords);
            //4.执行sql
             rs = ps.executeQuery();

            //5.处理查询结果集
            while (rs.next()){
                System.out.println(rs.getString(1));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //6.释放资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
  • 使用PreparedStatement完成insert、update、delete
package com.bipowernode;

import java.sql.*;

//使用PreparedStatement完成insert、update、delete
public class Test07 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            //1.创建驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","123");
            //3.获取预编译数据库对象
//            String sql = "insert into dept(deptno,dname,loc)values(40,'renshibu','xian')";
//            String sql = "update dept set dname = 'caiwubu',loc = 'beijing' where deptno = 40";
            String sql = "delete from dept where deptno = 40";

            ps = conn.prepareStatement(sql);
            //4.执行sql
            int count = ps.executeUpdate();
//            System.out.println(count == 1 ? "添加成功":"添加失败");
//            System.out.println(count == 1 ? "修改成功":"修改失败");
            System.out.println(count == 1 ? "删除成功":"删除失败");
            //5.处理查询结果集

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //6.释放资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
项目:使用 JavaScript 编写的杀死幽灵游戏(附源代码) 杀死鬼魂游戏是使用 Vanilla JavaScript、CSS 和 HTML 画布开发的简单项目。这款游戏很有趣。玩家必须触摸/杀死游荡的鬼魂才能得分。您必须将鼠标悬停在鬼魂上 - 尽量得分。鬼魂在眨眼间不断从一个地方移动到另一个地方。您必须在 1 分钟内尽可能多地杀死鬼魂。 游戏制作 这个游戏项目只是用 HTML 画布、CSS 和 JavaScript 编写的。说到这个游戏的特点,用户必须触摸/杀死游荡的幽灵才能得分。游戏会根据你杀死的幽灵数量来记录你的总分。你必须将鼠标悬停在幽灵上——尽量得分。你必须在 1 分钟内尽可能多地杀死幽灵。游戏还会显示最高排名分数,如果你成功击败它,该分数会在游戏结束屏幕上更新。 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox。要玩游戏,首先,单击 index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值