继续获取错误没有为参数1指定值

时间:2016-07-11 00:19:43

标签: java sql postgresql

所以我的目标是创建一个可以用来按标题和公司搜索的表格 当我在Preparedstatement searchbyTitle中使用OR语句时,如果我不使用它,则可以使用

我一直收到此错误:

No value specified for parameter 2.
    at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:216)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:244)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302)

CODE:

package film;
public class FilmIntro extends JFrame {
 //private JFrame FilmFrame;
 private JTextField txtSearch;
 private JPanel contentPane;
 private JTable table;
 private Connection con;
 //private int pare;
 //private JRadioButton rdbtnCop;
 //private JRadioButton rdbtnTitle;

 //Dimension dim = Toolkit.getDefaultToolkit().getScreenSize()
 //this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     //FilmIntro window = new FilmIntro();
     //window.FilmFrame.setVisible(true);
     FilmIntro FilmFrame = new FilmIntro();

     //FilmFrame.pack();
     FilmFrame.setLocationRelativeTo(null);
     FilmFrame.setVisible(true);

    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }



 public void searchbyCompany() { //SELECT filmtitle, filmyear, filmgenre, companyname FROM  production, film, company WHERE  LOWER(companyname) like LOWER (?)and production.companyid = company.companyid AND production.filmid = film.filmid

  Connection con = null;
  PreparedStatement pst = null;
  ResultSet rs2 = null;
  //String query = "SELECT filmtitle, filmyear, filmgenre, companyname FROM  production, film, company WHERE  LOWER(companyname) like LOWER (?)and production.companyid = company.companyid AND production.filmid = film.filmid";
  //con = DBConnect.getConnection();

  try {
   con = DBConnect.getConnection();
   //search company
   pst = con.prepareStatement("SELECT filmtitle, filmyear, filmgenre, companyname FROM  production, film, company WHERE  LOWER(companyname) like LOWER (?)and production.companyid = company.companyid AND production.filmid = film.filmid");
   pst.setString(2, txtSearch.getText() + "%");
   rs2 = pst.executeQuery();

   table.setModel(DbUtils.resultSetToTableModel(rs2));
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public void searchbyTitle() {

  Connection connection = null;
  PreparedStatement statement = null;
  ResultSet resultSet = null;

  try {
   connection = DBConnect.getConnection();
   statement = connection.prepareStatement("SELECT filmtitle, filmyear, filmgenre, companyname FROM production, film, company  WHERE (LOWER(filmtitle) like LOWER (?) OR " + "LOWER (filmgenre) like LOWER (?) )and production.companyid = company.companyid AND production.filmid = film.filmid");
   statement.setString(1, "%" + txtSearch.getText() + "%");
   resultSet = statement.executeQuery();
   table.setModel(DbUtils.resultSetToTableModel(resultSet));

  } catch (SQLException e) {
   e.printStackTrace();
  }


 }







 /**
  * Create the frame.
  */
 public FilmIntro() {
  setTitle("3D Movie System - Search ");

  setResizable(false);
  //FilmFrame = new JFrame();
  //private JFrame FilmFrame;
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(0, 0, 900, 700);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  contentPane.setLayout(null);
  setContentPane(contentPane);
  setLocationRelativeTo(null);
  JLabel lblSearchDatabase = new JLabel("Search Database");
  lblSearchDatabase.setForeground(Color.PINK);
  lblSearchDatabase.setHorizontalAlignment(SwingConstants.CENTER);
  lblSearchDatabase.setFont(new Font("Throw My Hands Up in the Air", Font.BOLD | Font.ITALIC, 55));
  lblSearchDatabase.setBounds(10, 21, 864, 88);
  getContentPane().add(lblSearchDatabase);





  JRadioButton rdbtnTitle = new JRadioButton("title");
  JRadioButton rdbtnCop = new JRadioButton("company");
  rdbtnCop.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    searchbyCompany();
    rdbtnTitle.setSelected(false);
   }
  });
  rdbtnCop.setBounds(167, 185, 109, 23);
  contentPane.add(rdbtnCop);


  rdbtnTitle.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    searchbyTitle();
    rdbtnCop.setSelected(false);



   }
  });
  rdbtnTitle.setBounds(559, 185, 109, 23);
  contentPane.add(rdbtnTitle);











  txtSearch = new JTextField();
  txtSearch.addKeyListener(new KeyAdapter() {
   @Override
   public void keyReleased(KeyEvent arg1) {
    if (rdbtnCop.isSelected()) {
     searchbyCompany();
    } else {
     searchbyTitle();


    }


   }
  });
  txtSearch.setBorder(javax.swing.BorderFactory.createEmptyBorder());
  txtSearch.addMouseListener(new MouseAdapter() {
   @Override
   public void mouseClicked(MouseEvent arg0) {
    txtSearch.setText("");

   }
  });
  txtSearch.setForeground(Color.LIGHT_GRAY);
  txtSearch.setText("Search for film,genre,production company etc...");
  txtSearch.setBounds(177, 148, 620, 30);
  getContentPane().add(txtSearch);
  txtSearch.setColumns(10);


  JLabel magnilbl = new JLabel("");
  Image mag = new ImageIcon(this.getClass().getResource("/magni.png")).getImage();
  magnilbl.setIcon(new ImageIcon(mag));
  magnilbl.setBounds(147, 148, 30, 30);
  getContentPane().add(magnilbl);


  JLabel lblOr = new JLabel("OR");
  lblOr.setForeground(Color.PINK);
  lblOr.setBounds(349, 189, 46, 14);
  getContentPane().add(lblOr);

  JButton btnViewAllFilms = new JButton("View all films");
  btnViewAllFilms.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {
    try {
     con = DBConnect.getConnection();

     String query = "SELECT filmtitle, filmyear, filmgenre, companyname FROM  production, film, company WHERE production.companyid = company.companyid AND production.filmid = film.filmid ORDER BY filmtitle";

     PreparedStatement ps = con.prepareStatement(query);
     ResultSet rs = ps.executeQuery();


     table.setModel(DbUtils.resultSetToTableModel(rs));

     //table.getColumnModel().getColumn(0).setPreferredWidth(200);
     //table.getColumnModel().getColumn(1).setPreferredWidth(50);
     //table.getColumnModel().getColumn(2).setPreferredWidth(150);
    } catch (Exception e) {
     e.printStackTrace();
    }

   }
  });

  btnViewAllFilms.setBounds(405, 185, 111, 23);
  btnViewAllFilms.setBackground(Color.BLACK);
  btnViewAllFilms.setForeground(Color.PINK);
  getContentPane().add(btnViewAllFilms);

  JScrollPane scrollPane = new JScrollPane();
  scrollPane.setBounds(46, 236, 802, 402);
  getContentPane().add(scrollPane);

  table = new JTable();
  scrollPane.setViewportView(table);


  Image umg = new ImageIcon(this.getClass().getResource("/bglogin.gif")).getImage();




  JLabel backgroundlbl = new JLabel("");
  backgroundlbl.setIcon(new ImageIcon(umg));
  backgroundlbl.setBounds(0, 0, 894, 671);
  contentPane.add(backgroundlbl);


  table.setDefaultEditor(Object.class, null);



 }
}

我是数据库新手,很抱歉,如果我错了。

1 个答案:

答案 0 :(得分:0)

如果您只有一个参数(例如searchbyCompany),那么它应该是

pst.setString(1, txtSearch.getText()+"%");

如果你有2个参数(比如searchbyTitle)

statement.setString(1, "%" +txtSearch.getText()+"%");
statement.setString(2, "%" +txtSearch.getText()+"%");