Null Pointer Exception for prepared statement Sql Connection in java

我的未来我决定 提交于 2020-01-25 11:00:28

问题


I have gone to same type of questions in this forum, but their solutions are not helping here. I am trying to run following code in java while trying to connect to MySql workbench:

import java.sql.*;

public class DBConnection {

public static void main(String[] args) 
{
    Connection con = null;
    PreparedStatement st =null;
    try
    {   
    Class.forName("com.mysql.jdbc.Driver"); 
    con  = DriverManager.getConnection("jdbc:mysql://localhost:3306/world","root","password"); 
    String sql = "select * from city where id = 1";
    st = con.prepareStatement(sql);            <---- here the error is shown
    ResultSet rs = st.executeQuery();
    while(rs.next())
    {
        int id = rs.getInt("ID");
        String Name = rs.getString("Name");
        String CountryCode = rs.getString("CountryCode");
        String District = rs.getString("District");
        int Population = rs.getInt("Population");
        System.out.println(id);
        System.out.println(Name);
        System.out.println(CountryCode);
        System.out.println(District);
        System.out.println(Population);         
    }
    rs.close();
    st.close();
    con.close();    
}catch(Exception e)
{
    e.printStackTrace();        
}finally
{
    try{
         if(st!=null)
            st.close();
      }catch(SQLException se2){
      }
      try{
         if(con!=null)
            con.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }
   System.out.println("Goodbye!");
}

I am getting the following error majorly:

 --> PreparedStatement.java:4063 com.mysql.jdbc.PreparedStatement.toString()
  --> PreparedStatement.java:587 com.mysql.jdbc.PreparedStatement.asSql()
   --> PreparedStatement.java:591 
com.mysql.jdbc.PreparedStatement.asSql(false)
java.lang.NullPointerException
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:649)
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:587)
at com.mysql.jdbc.PreparedStatement.toString(PreparedStatement.java:4068)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at com.mysql.jdbc.trace.Tracer.printParameters(Tracer.aj:240)
at com.mysql.jdbc.trace.Tracer.printEntering(Tracer.aj:167)
at com.mysql.jdbc.trace.Tracer.entry(Tracer.aj:126)
at    com.mysql.jdbc.trace.Tracer.ajc$before$com_mysql_jdbc_trace_Tracer$1$f51c62b8(Tracer.aj:45)
at com.mysql.jdbc.Connection.registerStatement(Connection.java)
at com.mysql.jdbc.Statement.<init>(Statement.java:270)
at com.mysql.jdbc.PreparedStatement.<init>(PreparedStatement.java:500)
at com.mysql.jdbc.Connection.clientPrepareStatement(Connection.java:2187)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4829)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4734)
at Filereader.DBConnection.main(DBConnection.java:19)

 --> Connection.java:2199 com.mysql.jdbc.Connection.close()
--> Connection.java:4892 com.mysql.jdbc.Connection.realClose(true, true, false, null)
--> Connection.java:4343 com.mysql.jdbc.Connection.isClosed()

What is wrong here in the code? As per my knowledge, prepared statement for DB Connectivity is written correct and Sql query format is also seems fine.

来源:https://stackoverflow.com/questions/29327841/null-pointer-exception-for-prepared-statement-sql-connection-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!