当前位置:   article > 正文

java get方法报空指针_面试的哪些事儿之JAVA程序员面试笔试题(一)

java getstring空指针

88544fd90e6a11fe4d9cdbc6f07571f1.gif

前言

在一个技术微信群看一个网友最近在一家公司做笔试的题目,然后我就整理了一下,供大家参考一下,希望能够帮助到大家。

97fc76b6edbc0df55edc39bf02bbb16a.png

笔试内容

1.假设有一个mysql实例,相关信息如下:

schema名为test用户名密码为6个b端口为:3306主机IP为:127.0.0.1该schema里有个一表叫person,请用jdbc从连接数据库开始到使用SQL取得person表中所有记录与过程。

Connection connection = null;        Statement statement = null;        ResultSet resultset = null;        //由于在finally代码块中需要关闭资源,所以在此处定义变量而不是在try代码块中(目的是将变量的作用范围提升,提高代码复用性)        try {            Class.forName("com.mysql.jdbc.Driver");//1、加载驱动            connection = DriverManager.getConnection("jdbc:mysql://192.168.0.1:7706/sky", "aaaaaa", "aaaaaa");//2、与数据库建立连接            statement = connection.createStatement();//3、创建SQL语句对象            String sql = "select * from person";//4、编写SQL语句            resultSet = statement.executeQuery(sql);//5、执行SQL语句            while(resultSet.next()) {                String id = resultSet.getString("id");                String name= resultSet.getString("name");                System.out.println(id + "," + name);//6、输出while循环遍历结果                //while循环工作原理:resultset.next()一开始指向结果集中第一行元素,之后每执行一次就往后跳动一次,为空时结束循环。            }        } catch (Exception e) {            e.printStackTrace();//7、释放资源(倒序)        } finally {            try {                if (resultSet != null) {//判断对象是否为空,因为如果在赋值之前该对象为空,不加if判断的话,此时会报空指针异常的错误。(下同)                    resultSet.close();                }            } catch (SQLException e) {                e.printStackTrace();            }            try {                if (statement != null) {                    statement.close();                }            } catch (SQLException e) {                e.printStackTrace();            }            try {                if (connection != null) {                    connection.close();                }            } catch (SQLException e) {                e.printStackTrace();            }        }

2.使用工厂模式实现传入不同的值返回结果:
传入参数为0,返回hello
传入参数为1,返回word
传入参数为2,返回java

//抽象接口类public interface StrInterface {        public String get();}//hello类public class Hello implements StrInterface{        //实现并重写父类的get()方法    public String get() {        return "hello";    }}//word类public class Word implements StrInterface{        //实现并重写父类的get()方法    public String get() {        return "word";    }}//java类public class Java implements StrInterface{    //实现并重写父类的get()方法    public String get() {        return "java";    }}//工厂类public class Factory {        public static StrInterface getStr(int param) {        StrInterface str = null;        if (0 == param) {            str = new Hello();        } else if (1 == param) {            str = new Word();        } else if (2 == param) {            str = new Java();        }        return str;    }}//main方法public static void main(String[] args) {    StrInterface hello = Factory.getStr(0);    System.out.println(hello.get());    StrInterface word = Factory.getStr(1);    System.out.println(word.get());    StrInterface java = Factory.getStr(2);    System.out.println(java.get());}

3.select count(*) 与 select count(1) 的区别。

如果你的数据表没有主键,那么count(1)比count()快

如果有主键的话,那主键(联合主键)作为count的条件也比count()要快

如果你的表只有一个字段的话那count()就是最快的啦

count() count(1) 两者比较。主要还是要count(1)所相对应的数据字段。

如果count(1)是聚索引,id,那肯定是count(1)快。但是差的很小的。

因为count(),自动会优化指定到那一个字段。所以没必要去count(?),用count(),sql会帮你完成优化的。

4.java类Person有name,age字段,假设一个集合List请根据 age 从大到小对这个list进行排序。

list.stream().sorted(Comparator.comparing(Person::getAge).reversed())

5.对List进行排序,集合中有数字1,1,3,3,4,5,6,2,8,1,5。

Listlist = Lists.newArrayList(1,1,3,3,4,5,6,2,8,1,5);Set set = new HashSet(list);List changeList = new ArrayList(set);

6.假设有个HashMap,使用两种方式遍历Map对象中的数据。

Map<String, Student> map = new HashMap<String, Student>();Iterator iterator = map.entrySet().iterator();while(iterator.hasNext()) {    Map.Entry set = (Map.Entry) iterator.next();    System.out.println(set.getKey()+","+set.getValue());}for (Map.Entry set:map.entrySet()) {    System.out.println(set.getKey()+","+set.getValue());}

7.JDBC中Statement与PrepareStatement的区别。prepareStatement会先初始化SQL,先把这个SQL提交到数据库中进行预处理,多次使用可提高效率。createStatement不会初始化,没有预处理,每次都是从0开始执行SQL

8.使用纯javascript判断用户在input type=“text” name=“password” 输入内容为数英混合(0-9+a-z+A-Z)

var input = document.getElementsByName("password");var reg=/^[A-Za-z0-9]+$/;   /*定义验证表达式*/if(!reg.test(input)){ alert("请输入英数组合字符"); return false;}

9.在linux上查看名为“mongodb” 的进程。

ps -ef |grep mongdb
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/320806
推荐阅读
相关标签
  

闽ICP备14008679号