简体   繁体   English

如何使用mysql查询处理null数据库

[英]How to handle null database using mysql query

This method is working fine. 这种方法很好用。 Here I'm fetching max bookid from book table but I'm getting null pointer exception when database is null. 在这里,我从书本表中获取最大书号,但是当数据库为空时,我得到了空指针异常。 So, how to handle this exception. 因此,如何处理此异常。

public void insertData() {        
            Transaction tx = null;
            Session session = getSession();
            try {
                tx = session.beginTransaction();
                Book book = new Book();
                SQLQuery query = session.createSQLQuery("select MAX(bookid) from books");
                List<Book> rows = query.list();
                for (Object row : rows) {
                    book.setBookid(Integer.parseInt(row.toString()));
                    Integer id1 = book.getBookid() + 1;                
                    selbookid = id1;
                }        

                book.setBookname("Java");            
                book.setDateofcreation(new Date());
                book.setLastmodifieddate(new Date());           

                session.save(book);
                tx.commit();
                session.flush();
                session.close();
            } catch (RuntimeException e) {   
                e.printStackTrace();
            } 
        }

When your query returns a single row/column value, use uniqueResult() instead of list(). 当查询返回单个行/列值时,请使用uniqueResult()而不是list()。

Hope the exception raises when there are no rows in table. 希望表中没有行时引发异常。 I've made few changes to your code. 我对您的代码进行了很少的更改。

Try this 尝试这个

       public void insertData() 
       {        
        Transaction tx = null;
        Session session = getSession();
        int result =0;
        try {
            tx = session.beginTransaction();
            Book book = new Book();
            SQLQuery query = session.createSQLQuery("select MAX(bookid) from books");
           try
           {
            result = (int)query.uniqueResult();   
           }
           catch(Exception e)
           {               
           //do whatever you want with exception
           }
            book.setBookid(result+1);
            book.setBookname("Java");            
            book.setDateofcreation(new Date());
            book.setLastmodifieddate(new Date());           

            session.save(book);
            tx.commit();
            session.flush();
            session.close();
        } 
        catch (RuntimeException e) {   
            e.printStackTrace();
        } 
       }

Your question is not clear but what can I see in your question you are saying if database is null then only you are getting execption. 您的问题尚不清楚,但是我在您的问题中看到的是如果数据库为空,那么只有您才能执行。

I check this method in my system and found why you are getting exception because in SQL query you are fetching max value of bookid from books table but if database is null then how will be fetch max value so you need to do some changes in query. 我在系统中检查了此方法,发现为什么会出现异常,因为在SQL查询中您正在从books表中获取bookid的最大值,但是如果数据库为null,那么将如何获取maxid的值,因此您需要在查询中进行一些更改。 You should use IFNULL condition like: 您应该使用IFNULL条件,例如:

select IFNULL(MAX(bookid),1) from books

It is working for me even database is null because inserting bookid 1 if books table is empty. 即使数据库为空,它也对我有用,因为如果books表为空,则插入bookid 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM