佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 754|回复: 9

java 问题....第十楼。。。

[复制链接]
发表于 23-10-2006 09:47 PM | 显示全部楼层 |阅读模式
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;


public class getPaymentID {
          private Connection connect;

          
          String url = "jdbcdbc:test";  
             

      // Load the driver to allow connection to the database
     
          
        public void getPaymentID(){
  
                 try {
                 Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

                 connect = DriverManager.getConnection(url);
              }
              catch ( ClassNotFoundException cnfex ) {
                 System.err.println(
                    "Failed to load JDBC/ODBC driver." );
                 cnfex.printStackTrace();
                 System.exit( 1 );  // terminate program
              }
              catch ( SQLException sqlex ) {
                 System.err.println( "Unable to connect" );
                 sqlex.printStackTrace();
              }
                  
      
                try {
       
                        Statement statement =connect.createStatement();
                        String query2 = "SELECT PPaymentID FROM Paymenet ORDER BY PPaymentID  ";

                        ResultSet rs = statement.executeQuery( query2 );
                        try {        
                                rs.next();
                        String        findcID1=rs.getString(1);
                                if ( findcID1 != null ) {
                                        System.out.println(findcID1);
                               



                                }//end if !=null
                                else   
                                        JOptionPane.showMessageDialog(null,"Record Not Exist","Fail",JOptionPane.ERROR_MESSAGE);   
                        }
                        catch ( SQLException sqlex ) {

                                JOptionPane.showMessageDialog(null,"Record Not Exist","Fail",JOptionPane.ERROR_MESSAGE);
                        }


                        statement.close();
                }
                catch ( SQLException sqlex ) {
                        JOptionPane.showMessageDialog(null,"Incorrect Payment ID","Fail",JOptionPane.INFORMATION_MESSAGE);
                }




}

}





为什么其他class拿不到findcID1 data?

[ 本帖最后由 lonely2 于 26-10-2006 11:54 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 23-10-2006 11:19 PM | 显示全部楼层
變數生命週期的問題,因為你定義在

public void getPaymentID(){
    xxx
    xxx
    String findcID1 = xxx ;
    xxx
}

所以這個變數之能夠存活在getPaymentID()被呼叫的時候,當程式執行getPaymentID()完畢,這個變數就會等待被回收,也就是沒有參考(reference)指向它,
簡單的說如果你要定義成別人可以看到的話,至少要定義在
public class xxx{
   String findcID1 ;

   public void getPaymentID(){
       xxx
       xxx
       findcID1 = xxx;
       xxx
   }

}
這樣這個變數就能撐到物件的生命週期結束.

當然權限要在 private 之上,而這樣寫有沒有違反物件導向的精神,那麼又是另外一個故事了......

[ 本帖最后由 莫名奇妙 于 23-10-2006 11:26 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 23-10-2006 11:27 PM | 显示全部楼层
原帖由 莫名奇妙 于 23-10-2006 11:19 PM 发表
變數生命週期的問題,因為你定義在

public void getPaymentID(){
    xxx
    xxx
    String findcID1 = xxx ;
    xxx
}

所以這個變數之能夠存活在getPaymentID()被呼叫的時候,當程式執行getPaymentI ...

谢谢。。解决了。。
回复

使用道具 举报

 楼主| 发表于 24-10-2006 12:42 AM | 显示全部楼层
smd7=rs3.getString(10);
                                                                                smd8=rs3.getString(11);
                                                                                smd9=rs3.getString(12);
                                                                                smd10=rs3.getString(13);
                                                                                String smodule1[]={"'smd1'","'smd2'","'smd3'"};
                                                                                jcbsmModule.addItem(smodule1);       


我可以这样把getstring data 这样pass into smodule string ma??

怎么。。。 jcbsmodule 没有display getstring data..只是display java.lang.string:@  ?? 什么问题???
回复

使用道具 举报

发表于 24-10-2006 04:36 PM | 显示全部楼层
厄.....我不知道你的 smodule1 裡面是要放什麼東西,如果你要放的東西是 smd1 裡面的值的話,那麼你這個寫法錯了,因為 print 出來的結果會變成 'smd1' , 因為你指定字串給它,如果你要他 print 出來的結果是資料庫拿出來的值.你可以這樣寫

String smodule1[]={smd1,smd2,smd3};

這樣他才會把smd1的內容放進去,

另外,你會出現 java.lang.string 的原因是不是因為你直接print array ??

我沒有看到你的 print 的 code , 所以不能確定,但是我推測是你直接把 array print 出來了,如下:

System.out.println(smodule1);

所以當然出現 java.lang.string

你可以用 System.out.println(smodule1[0]); 來指定他要 print array 裡面的哪一個 ...
回复

使用道具 举报

 楼主| 发表于 24-10-2006 07:37 PM | 显示全部楼层
原帖由 莫名奇妙 于 24-10-2006 04:36 PM 发表
厄.....我不知道你的 smodule1 裡面是要放什麼東西,如果你要放的東西是 smd1 裡面的值的話,那麼你這個寫法錯了,因為 print 出來的結果會變成 'smd1' , 因為你指定字串給它,如果你要他 print 出來的結果是資料庫拿 ...




er...我的意思是--->

string query select * from module ;
ResultSet rs = statement.executeQuery( query );
                   try {        
                            rs.next();

smd7=rs3.getString(10)
smd8=rs3.getString(11);
smd9=rs3.getString(12);
smd10=rs3.getString(13);
String smodule1[]={"'smd1'","'smd2'","'smd3'"};
jcbsmModule.addItem(smodule1);       


---------------------------------------------------------
jcbsmModule<--- combo box
smd8,smd8,smd9,smd10<--- string data.

怎样才可以把smd8,smd8,smd9,smd10 data 放进combo box 里面?
回复

使用道具 举报

Follow Us
 楼主| 发表于 24-10-2006 08:47 PM | 显示全部楼层
还有一个问题不明白。。。

original database data 是-->
ID   Name  Age
1    halo   12

之后我改了
ID   Name  Age
2    haha   20


但为什么我从java program拿出来的data还是

ID   Name  Age
1    halo   12
而不是


ID   Name  Age
2    haha   20


???为什么?????
回复

使用道具 举报

发表于 25-10-2006 09:39 AM | 显示全部楼层
原帖由 lonely2 于 24-10-2006 07:37 PM 发表
er...我的意思是--->

string query select * from module ;
ResultSet rs = statement.executeQuery( query );
                   try {        
                            rs.next();

smd7=rs3.getString(10)
smd8 ...


jcbsmModule.addItem(smd7);
jcbsmModule.addItem(smd8);
jcbsmModule.addItem(smd9);
jcbsmModule.addItem(smd10);

就行了!

原帖由 lonely2 于 24-10-2006 08:47 PM
还有一个问题不明白。。。

original database data 是-->
ID   Name  Age
1    halo   12

之后我改了
ID   Name  Age
2    haha   20
...


在你还没execute 你的update statement, double check 下你passed in 的values 是否正确,如果正确, 那就check 是否把autocomit set to false, 然后有没自己commit.
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 25-10-2006 12:27 PM | 显示全部楼层
原帖由 黑木头 于 25-10-2006 09:39 AM 发表


jcbsmModule.addItem(smd7);
jcbsmModule.addItem(smd8);
jcbsmModule.addItem(smd9);
jcbsmModule.addItem(smd10);


就行了!



在你还没execute 你的update statement, double check 下你passed i ...

q1..没有想到是那么容易的

q2...今天那个database没有了。。我也不知道什么回事



3。。。。。谢谢你的指点。。谢谢//
回复

使用道具 举报

 楼主| 发表于 26-10-2006 11:54 PM | 显示全部楼层
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.table.*;
import java.awt.print.*;

import java.net.URL;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class password   {
       
         Connection connect;
        String passString,userString,url;
        JButton pwOK,pwCancel;
        JPanel passPane,passPanel,userPanel,panBT,panDialog;
        JTextField userField,logUser;
        JLabel passLabel,userLabel;
        JPasswordField passField;
        JDialog passDialog;
        JOptionPane error;
       
       
       
        public void password(){

                   JOptionPane.showMessageDialog(null,"Record Not Exist","Warning",
                   JOptionPane.ERROR_MESSAGE);            

            //************************** password dialog
            
             try{        
                     doConn();
             } catch(Exception e) {
                 System.out.println("Error : " + e.getMessage());
                }      
          
             pwOK=new JButton("OK" );
             pwOK.setToolTipText("Logon to system" );
             pwOK.setMnemonic(KeyEvent.VK_O);
             pwCancel=new JButton("Cancel" );
             pwCancel.setToolTipText("Close window" );
             pwCancel.setMnemonic(KeyEvent.VK_C);
            
          
             JPanel panPic=new JPanel();
             panPic.setBackground(Color.white);
            
             userField=new JTextField(15);
             userLabel=new JLabel(" Please enter username : " );
             passField=new JPasswordField(15);
             passLabel=new JLabel( "   Please enter password : " );
             passDialog=new JDialog();
             passPanel=new JPanel();
             passPanel.setBackground(Color.white);
             passPanel.setLayout(new BoxLayout(passPanel,BoxLayout.X_AXIS));
             passPanel.add(passLabel);
             passPanel.add(passField);
             passPanel.setBorder(BorderFactory.createEmptyBorder(10,0,10,10));
             userPanel=new JPanel();
             userPanel.setBackground(Color.white);
             userPanel.setLayout(new BoxLayout(userPanel,BoxLayout.X_AXIS));
             userPanel.add(userLabel);
             userPanel.add(userField);
             panBT=new JPanel();
             panBT.setBackground(Color.white);
             panBT.setLayout(new BoxLayout(panBT,BoxLayout.X_AXIS));
             panBT.add(pwOK);
             panBT.add(pwCancel);
             panBT.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
             panDialog=new JPanel();
             panDialog.setBackground(Color.white);
             panDialog.add(panPic);
             panDialog.add(userPanel,BorderLayout.NORTH);
             panDialog.add(passPanel,BorderLayout.CENTER);
             panDialog.add(panBT,BorderLayout.SOUTH);
             passDialog.setContentPane(panDialog);
             passDialog.getRootPane().setDefaultButton(pwOK);
             passDialog.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                   System.exit(0);
             }
             });
             passDialog.setSize(400,350);
             passDialog.setResizable(false);
             passDialog.setVisible(true);
             passDialog.setTitle( "Authentication" );
             passDialog.setLocation(200,100);


          //************************** pwCancel button event handler

          pwCancel.addActionListener(new ActionListener()  {
          public void actionPerformed(ActionEvent ae) {
              System.exit(0);
          }
          }
          );

         //************************** pwOK button event handler

         pwOK.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
              try{
                     passString=new String(passField.getPassword());
                     userString=new String(userField.getText());
                         
                       if(checkUP(userString,passString)){     // passing username and password to method and do checking
                         passDialog.setVisible(false);                          
                                  main main = new Main();
                        
                         logUser.setText(userString.toUpperCase());
                        
                       }
                       else {                                   // if username and password incorrect
                         userField.setText( "" );
                         passField.setText( "" );
                         error=new JOptionPane();
                         error.showMessageDialog(null,"Invalid username or password. Please try again! ","Error",JOptionPane.ERROR_MESSAGE);
                         }
                    } catch(Exception e) {
                       System.out.println(" Error : " + e.getMessage());
                       }
                      
            }});

        }//end doLogin

         
        private boolean checkUP(String u,String p) throws Exception {
                 Statement statement =connect.createStatement();
                 statement=connect.createStatement();
                 ResultSet rs = statement.executeQuery( "Select * from User where user='"+userString+"' and pass='"+passString+"'" );  
          
             while(rs.next()){
                return true;
             }
              return false;

         } //end checkup
       
       
        public void doConn(){
                try {
                 url = "jdbc: odbc:Student";

                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver" );
                 connect = DriverManager.getConnection( url );
                             }
              catch ( ClassNotFoundException cnfex ) {
                               cnfex.printStackTrace();
                              }
              catch ( SQLException sqlex ) {
                                 sqlex.printStackTrace();
                           }
              catch ( Exception ex ) {
                 // process others Exceptions
                 ex.printStackTrace();
                         }
                               
        }//end doConn
       

           public static void main(String args[]){
                                  
                   password a=new password();
                                  
           }

}//end method





为什么,没有东西displau出来>_<.
回复

使用道具 举报

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

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


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

GMT+8, 23-8-2025 02:49 AM , Processed in 0.125620 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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