橙子cch
2015-05-17 10:35:09
等
java jdbc与mysql为什么连接不上并且抛出空指针异常?
jdbc连接代码:
public class ConnDB{ Connection conn=null; Statement stmt=null; ResultSet rs=null; public ConnDB(){ try{ Class.forName("com.mysql.jdbc.Driver"); }catch(java.lang.ClassNotFoundException e){ System.err.println(e.getMessage()); } } /*************************************************** *method name: executeQuery() *功能:执行查询操作 *return value: ResultSet * @throws ClassNotFoundException ****************************************************/ public ResultSet executeQuery(String sql) { try{ // conn=getConnection(); // Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/database?user=root&password=root&useUnicode=true"); //JDBConnection connection=new JDBConnection(); //conn=connection.getConnection(); stmt=conn.createStatement(); rs=stmt.executeQuery(sql); }catch(Exception ex){ System.err.println(ex.getMessage()); }finally{} return rs; } /*************************************************** *method name: close() *功能:关闭数据库链接 *return value: void ****************************************************/ public void close(){ try { if (rs != null) rs.close(); } catch (Exception e) { e.printStackTrace(System.err); }finally{ try { if (stmt != null) stmt.close(); } catch (Exception e) { e.printStackTrace(System.err); }finally{ try { if (conn != null) { conn.close(); } } catch (Exception e) { e.printStackTrace(System.err); } } } } } 查询代码: public int checkManager(String name,String inpwd) { int flag = 0; ConnDB conn=new ConnDB(); String sql = "SELECT * FROM admin where username='" +name + "'"; System.out.println(sql); ResultSet rs = conn.executeQuery(sql); try { if (rs.next()) { String pwd = inpwd; if (pwd.equals(rs.getString(3))) { flag = 1; rs.last(); int rowSum = rs.getRow(); rs.first(); if (rowSum != 1) { flag = 0; System.out.print("获取的row的值:" + sql + rowSum); } } else { flag = 0; } }else{ flag = 0; } } catch (SQLException ex) { flag = 0; } // ConnDB.close(rs, st, conn); conn.close(); return flag; } 在 if (rs.next()) 处报空指针
评论

shiyun LV6
2015年5月17日
应该是 conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root","root");
报空指针很好解决的,看下有没有相应的jar包,看数据库有没有连接上,跟踪下,检查下就知道了
- 等 最代码怎么获取牛币啊?
- 完 谁来告诉我最代码上线的时间,答对者给5牛币,先来先得
- 等 牛友们,大家好,你们做程序员多久了?现在还好吗?
- 完 在微信打开的页面里进行app下载
- 等 最代码2014年欢乐聚声会
- 完 mysql如何查询表数据并且对3个字段降序的SQL?
- 完 最代码牛币机制改革
- 完 成功的在bae上使用了自定义运行环境 jetty+nginx的组合,大家对jetty+nginx优化有哪些心得?
- 完 进来分享一下各位牛牛是如何加入最代码大家庭的?
- 等 为什么java BufferedImage类处理大图直接抛出内存溢出的异常?
- 等 最代码是否开发手机app客户端?
- 完 java程序员学习哪些java的技术?java有哪些框架?都能做哪方面的开发?
- 等 php格式网页文件怎么运行?
- 等 Java volatile值获取的问题
- 等 前端vue,拦截了登录后台后,返回的token,requests拦截token,但是发送请求的时候,就出现跨越异常
- 等 大专本科计算机科班怎么找到Java工作?
- 等 eclipse怎么把三个java swing游戏项目合成一个项目?
- 完 伙伴们,大家都有什么好的解压方式么,分享一下~
- 完 三四线城市,6、7k,运维工作,索然无味,想去辞职上培训,各位牛牛有什么建议嘛
- 等 jsp页面输入中文变成问号
- 等 JPA在线上运行一段时间后报错Caused by: java.lang.IncompatibleClassChangeError: null
- 等 PHP 这个规则用preg_match_all怎么写
- 等 大佬们,有没有知道Alfresco如何配置LDAP登录呢?
- 等 php的install目录是框架带的吗?


a1021003714
2020年6月29日
暂无贡献等级
765105637 LV9
2020年6月24日
Quicty
2020年6月9日
暂无贡献等级
dasdsss
2020年5月7日
暂无贡献等级
longqingli
2020年4月23日
暂无贡献等级
YuYuLoveYou
2019年11月26日
暂无贡献等级
易新城 LV11
2019年10月20日
林春青 LV2
2019年9月9日
老痰酸菜丶
2019年9月3日
暂无贡献等级
1324406184 LV1
2019年6月12日