JDBC空指针异常,求帮助!(40分)

搬家中的程序猿 2014-01-05 08:46:44

public class checkmessage extends JFrame implements ActionListener{

private JTextField searchfield;
private JLabel searchlabel ;
private JButton button1;
private JPanel jp;

public checkmessage()
{
this.setTitle("影片查询");
jp=new JPanel();
searchlabel=new JLabel("影片ID查询");
searchfield=new JTextField("请输入要查询的ID");

button1=new JButton("查询");
JLabel searchlabel2 = new JLabel("按Id查询");

jp.setLayout(new FlowLayout());
jp.add(searchlabel);
jp.add(searchfield);
jp.add(button1);
this.setLayout(new BorderLayout());
this.add(jp,BorderLayout.NORTH);


button1.addActionListener(this);
this.setSize(300,100);
this.setVisible(true);
}

public void actionPerformed (ActionEvent a)
{
String searchid,sql;
searchid=searchfield.getText().trim();

if(searchid.equals(" ")){
JOptionPane.showMessageDialog(null,"查询条件不能为空","查询",JOptionPane.WARNING_MESSAGE);
}
else
{
sql="select * from movie where ID='"+ searchid +"'";
try{
ResultSet conn=new jdbcfile().executeQuery(sql);
while(conn.next())
{
String id=conn.getString("ID");
String date=conn.getString("DATE");
String name=conn.getString("NAME");
String deparment=conn.getString("DEPARMENT");


new checkt(id,name,date,deparment);
}
}
catch(Exception el){
System.out.println(el);
}


}
}

public static void main(String[] args)
{
checkmessage ap=new checkmessage();

}
}

...全文
204 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
这是数据库里的 package com.movie.sql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class jdbcfile { Connection conn; Statement stmt; int inorupdatevalue=-1; //驱动 链接 执行语句 关闭连接 static String url = "jdbc:mysql://localhost:3306/movie"; static String username = "root"; static String password = "qwer"; public void getConnection(){ try { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //建立连接 conn = DriverManager.getConnection(url,username,password); stmt = conn.createStatement(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //执行sql语句 public ResultSet executeSQL(String sql){ try { stmt.executeUpdate(sql); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } //定义查询数据的方法 public synchronized ResultSet executeQuery(String sql)throws Exception{ ResultSet rs=stmt.executeQuery(sql); return rs; } //定义插入的方法 public synchronized int insert(String sql)throws Exception{ inorupdatevalue=stmt.executeUpdate(sql); return inorupdatevalue; } //定义修改数据的方法 public synchronized int update(String sql)throws Exception{ inorupdatevalue=stmt.executeUpdate(sql); return inorupdatevalue; } //定义删除的方法 public synchronized int del(String sql)throws Exception{ inorupdatevalue=stmt.executeUpdate(sql); return inorupdatevalue; } //定义关闭数据库链接的方法 public void close()throws Exception{ conn.close(); } }
coolbamboo2008 2014-01-05
  • 打赏
  • 举报
回复
ResultSet conn=new jdbcfile().executeQuery(sql); 这个new jdbcfile()里面有问题,估计有一项没初始化
姜小白- 2014-01-05
  • 打赏
  • 举报
回复
 sql="select * from movie where ID='"+ searchid +"'";
  try{
ResultSet conn=new jdbcfile().executeQuery(sql);
没看到楼主哪里去的数据库连接会话Connection,也没有看到statment,不理解楼主的写法
  • 打赏
  • 举报
回复
有木有另一种调用查询的方法???这个方法是别人用的。我实在是用不明白了
tony4geek 2014-01-05
  • 打赏
  • 举报
回复
debug 断点看看。
姜小白- 2014-01-05
  • 打赏
  • 举报
回复

conn = DriverManager.getConnection(url,username,password);

stmt = conn.createStatement();
从conn 开始断点,看conn 有没有拿到,看stmt 有没有创建成功

62,621

社区成员

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

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