佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1562|回复: 11

求救 java 问题

[复制链接]
发表于 19-7-2012 03:59 PM | 显示全部楼层 |阅读模式
本帖最后由 Kelvin017 于 19-7-2012 04:23 PM 编辑

各位大大, 小弟在 java connect to ms access 出现问题, 希望大大们能帮忙看看问题在哪里, 谢谢
当我要 search 的时候竟然 pop out error in SQL statement
由于太多表情了, 小弟上传在 mediafire ---> http://www.mediafire.com/?4bihz1p141dm37w

        private void initializeDB(){
            try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver";
                Connection conCustomer = DriverManager.getConnection("jdbcdbcustomerDB";
                stmtCustomer = conCustomer.createStatement();
            }
            catch(Exception ex){
                JOptionPane.showMessageDialog(null, "Error in connection to Database";
               
               }
        }
        
        private void findCustomer(ActionEvent e){
            String custID = jtfCustomerID.getText();           
            String sqlSelect = "Select * From Customers Where cust_ID = '" + custID + "'";           
            try{
                ResultSet rsCustomer = stmtCustomer.executeQuery(sqlSelect);
               
                if(rsCustomer.next()){
                    jlblCustomerName.setText(rsCustomer.getString("Cust_Name");
                    jlblCustomerAddress.setText(rsCustomer.getString("Cust_Address");
                }
                else{
                    JOptionPane.showMessageDialog(null, "Customer not found";
                }
            }
            catch(SQLException ex){
                JOptionPane.showMessageDialog(null, "Error in SQL Statement";
            }
        }
回复

使用道具 举报


ADVERTISEMENT

发表于 19-7-2012 04:12 PM | 显示全部楼层
很多符号。。看到很辛苦~   
btw,我帮不到你。。paiseh~
回复

使用道具 举报

 楼主| 发表于 19-7-2012 04:22 PM | 显示全部楼层
很多符号。。看到很辛苦~   
btw,我帮不到你。。paiseh~
碟立飞 发表于 19-7-2012 04:12 PM

不好意思, 我已经上传去 mediafire 了
http://www.mediafire.com/?4bihz1p141dm37w
回复

使用道具 举报

发表于 19-7-2012 04:52 PM | 显示全部楼层
一行一行写 system.out.println.. 那就知道实际上哪一行出问题了..
尤其是 sqlSelect, print 出来看看有问题没..
回复

使用道具 举报

发表于 19-7-2012 05:32 PM | 显示全部楼层
各位大大, 小弟在 java connect to ms access 出现问题, 希望大大们能帮忙看看问题在哪里, 谢谢
当我 ...
Kelvin017 发表于 19-7-2012 03:59 PM


放上你的log
回复

使用道具 举报

发表于 20-7-2012 12:59 PM | 显示全部楼层
可以用code来显示的,
  1. 像这样
复制代码
, 就不会乱了。

做么不把你的catch exception放在你的showmessagedialog,反而用custom msg?你放在那边不就知道哪里错了咯。
回复

使用道具 举报

Follow Us
发表于 21-7-2012 06:57 PM | 显示全部楼层

目测你的这行sql statement已经错了。

private void findCustomer(ActionEvent e){
     String custID = jtfCustomerID.getText();
     String sqlSelect = "Select * From Customers Where cust_ID = '" + custID + "'";
     try{
          ResultSet rsCustomer = stmtCustomer.executeQuery(sqlSelect);}

正确的应该是:"SELECT * FROM Customers WHERE cust_ID = '"+custID+"';"


有错请插
回复

使用道具 举报

发表于 24-7-2012 05:14 PM | 显示全部楼层
你的cust_ID 在资料库里面是vachar 吗?
一般我建议使用
String sql = String.format("SELECT * FROM Customers WHERE cust_ID = '%s';",custID);
小心你的开关引号........
回复

使用道具 举报


ADVERTISEMENT

发表于 15-8-2012 12:02 PM | 显示全部楼层
Jason929 发表于 21-7-2012 06:57 PM
目测你的这行sql statement已经错了。

SQL在JDBC 不能放;的。

插你了:)
回复

使用道具 举报

发表于 15-8-2012 05:08 PM | 显示全部楼层
很容易SQL Injection的样子。
回复

使用道具 举报

发表于 18-8-2012 03:36 AM | 显示全部楼层
I help change  and i give you example my code

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Administrator
*/
public class SearchCustomer extends javax.swing.JFrame {
public Connection conn=null;
public Statement stm=null;
public ResultSet rs=null;
public String Driver = "sun.jdbc.odbc.JdbcOdbcDriver";
public String Connect = "jdbcdbcustomerDB";
String name,address;

    /**
     * Creates new form SearchCustomer1
     */
    public SearchCustomer() {
        try
        {
           Class.forName(Driver);
           conn=DriverManager.getConnection(Connect);
           stm=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        }
        catch(Exception e)
        {
             System.out.println("Exception:"+e.getMessage());
        }
        initComponents();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Customer ID";

        jLabel3.setText("Customer Address";

        jTextField2.setText(" ";

        jLabel2.setText("Customer Name";

        jTextField3.setText(" ";

        jButton2.setText("Clear";

        jButton1.setText("Search Customer";
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(60, 60, 60)
                            .addComponent(jTextField1))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField2)
                            .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))))
                .addContainerGap(148, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                          

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        int cusid = Integer.parseInt(jTextField1.getText());   
         try
        {
            stm = conn.createStatement();
            String sql = "Select * from Customers WHERE Cust_ID='" +cusid + "'";
            ResultSet rs = stm.executeQuery(sql);
            if (rs.next()){
            name=rs.getString(2);
            address=rs.getString(3);              
            jTextField2.setText(name);
            jTextField3.setText(address);


             }
            else
            {
              JOptionPane.showMessageDialog(null,"Data Not Found! Please Try Again With A Valid Customer ID";
            }
        }
        catch(Exception e)
        {
              JOptionPane.showMessageDialog(this,e,"ERROR",JOptionPane.ERROR_MESSAGE);
        }


    }                                       

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SearchCustomer().setVisible(true);
            }
        });
    }
本帖最后由 jambo123 于 18-8-2012 04:41 AM 编辑

回复

使用道具 举报

发表于 18-8-2012 05:39 PM | 显示全部楼层
String sqlSelect = "Select * From Customers Where cust_ID = '" + custID + "'";      

符号放错了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 16-10-2025 12:47 PM , Processed in 0.120615 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表