java.lang.NullPointerException问题,链接mysql数据库的!

aa864061475 2018-11-29 09:13:28
我出了个空指针的问题,我看代码也没有显示错误啊,一个登陆 一个注册 只要跟数据库一有关系 就报这个500的空指针问题。求解啊!
...全文
1588 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
aa864061475 2018-11-30
  • 打赏
  • 举报
回复
感谢各位的帮助,问题解决了,总结下
1、把mysql加入系统变量
2、在url后面加上:useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

其实是特别简单的小问题,但是限于自己基础薄弱,没有发现。感谢大家的悉心解答,谢谢!
aa864061475 2018-11-30
  • 打赏
  • 举报
回复
引用 8 楼 Silly-77 的回复:


报错空指针异常 定位到39行 也就是说 你在使用conn的时候 conn是个空对象,所以报错了,你的连接创建失败了。
那就你就要看一下你的init()是否执行成功了。
启动tomcat的时候注意报错 可能是驱动加载失败 也可能是 jdbc配置错了



确实是mysql初始化失败了,我去网上查了下,人家说要把mysql-connector放到tomcat的lib目录下,但是还是不行 啊。我的mysql-connector版本8.0.13,数据库是8.0.11的。
  • 打赏
  • 举报
回复
如果两个都不懂,那就换一个最简单的

将 static Connection conn = null; 改为
static Connection conn = DriverManager.CreateConnection();
  • 打赏
  • 举报
回复
或者修改下连接方式
import java.sql.*;

public class Jdbc {
private static Connection Conn;
//这一句起了什么作用 为什么没有这句运行就错误
public static void main(String [] args){
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=mydate";
String userName="sa";
String userPwd="lzm0922";
try{
Class.forName(driverName);
Connection Conn=DriverManager.getConnection(dbURL,userName,userPwd);
System.out.println("连接数据库成功");
Statement stmt = Conn.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM BOOK");
while(rs.next()){
String Bno=rs.getString("Bno");
System.out.println(Bno);
System.out.println(rs.getString("Bname"));
}
rs.close();
stmt.close();
Conn.close();
}
catch(Exception e){
e.printStackTrace();
System.out.print("连接失败");
}
}
}
  • 打赏
  • 举报
回复
static Connection conn= new Connection() {

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}

@Override
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
// TODO Auto-generated method stub

}

@Override
public void setTransactionIsolation(int level) throws SQLException {
// TODO Auto-generated method stub

}

@Override
public void setSchema(String schema) throws SQLException {
// TODO Auto-generated method stub

}

@Override
public Savepoint setSavepoint(String name) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public Savepoint setSavepoint() throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public void setReadOnly(boolean readOnly) throws SQLException {
// TODO Auto-generated method stub

}

@Override
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
// TODO Auto-generated method stub

}

@Override
public void setHoldability(int holdability) throws SQLException {
// TODO Auto-generated method stub

}

@Override
public void setClientInfo(String name, String value) throws SQLClientInfoException {
// TODO Auto-generated method stub

}

@Override
public void setClientInfo(Properties properties) throws SQLClientInfoException {
// TODO Auto-generated method stub

}

@Override
public void setCatalog(String catalog) throws SQLException {
// TODO Auto-generated method stub

}

@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
// TODO Auto-generated method stub

}

@Override
public void rollback(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub

}

@Override
public void rollback() throws SQLException {
// TODO Auto-generated method stub

}

@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub

}

@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public CallableStatement prepareCall(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public String nativeSQL(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean isValid(int timeout) throws SQLException {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isReadOnly() throws SQLException {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isClosed() throws SQLException {
// TODO Auto-generated method stub
return false;
}

@Override
public SQLWarning getWarnings() throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public int getTransactionIsolation() throws SQLException {
// TODO Auto-generated method stub
return 0;
}

@Override
public String getSchema() throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public int getNetworkTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}

@Override
public DatabaseMetaData getMetaData() throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public int getHoldability() throws SQLException {
// TODO Auto-generated method stub
return 0;
}

@Override
public String getClientInfo(String name) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public Properties getClientInfo() throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public String getCatalog() throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean getAutoCommit() throws SQLException {
// TODO Auto-generated method stub
return false;
}

@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public Statement createStatement() throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public SQLXML createSQLXML() throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public NClob createNClob() throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public Clob createClob() throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public Blob createBlob() throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
// TODO Auto-generated method stub
return null;
}

@Override
public void commit() throws SQLException {
// TODO Auto-generated method stub

}

@Override
public void close() throws SQLException {
// TODO Auto-generated method stub

}

@Override
public void clearWarnings() throws SQLException {
// TODO Auto-generated method stub

}

@Override
public void abort(Executor executor) throws SQLException {
// TODO Auto-generated method stub

}
};
liuhr5211314 2018-11-30
  • 打赏
  • 举报
回复
哪行报错看哪行,结合上下文,很快就能找到问题在哪里
Silly-77 2018-11-29
  • 打赏
  • 举报
回复


报错空指针异常 定位到39行 也就是说 你在使用conn的时候 conn是个空对象,所以报错了,你的连接创建失败了。
那就你就要看一下你的init()是否执行成功了。
启动tomcat的时候注意报错 可能是驱动加载失败 也可能是 jdbc配置错了
aa864061475 2018-11-29
  • 打赏
  • 举报
回复
引用 4 楼 CJ95YD 的回复:
下次看异常栈时注意一下,问题一般出现在最上面一条你自己写的文件,这里就是DBconn.java这个文件

怎么看都感觉是对的- -太可怕了
aa864061475 2018-11-29
  • 打赏
  • 举报
回复
引用 5 楼 咸哼酒家 的回复:
DBconn类里面的conn对象为空额,初始化它


我给他值了呀!
咸哼酒家 2018-11-29
  • 打赏
  • 举报
回复
DBconn类里面的conn对象为空额,初始化它
CJ95YD 2018-11-29
  • 打赏
  • 举报
回复
下次看异常栈时注意一下,问题一般出现在最上面一条你自己写的文件,这里就是DBconn.java这个文件
aa864061475 2018-11-29
  • 打赏
  • 举报
回复
引用 1 楼 咸哼酒家 的回复:
UserDaoImpl.java 20行
LoginServlet.java 24行
的代码能贴一下吗
aa864061475 2018-11-29
  • 打赏
  • 举报
回复
咸哼酒家 2018-11-29
  • 打赏
  • 举报
回复
UserDaoImpl.java 20行
LoginServlet.java 24行
的代码能贴一下吗
  • 打赏
  • 举报
回复
static connection

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧