พบกับบทความเกี่ยวกับคอมพิวเตอร์และ programming ได้ที่ http://articles.modoeye.com | บล็อกอาหาร
Java programming / OutputStream ครับ
bit69
bit69
Wed 25 Jan 2006 23:48:18

คือตาม code ข้างล่างนี้นะครับ

import java.awt.*;
import java.awt.event.*;
import java.lang.System;
import java.io.*;


public class ReadWrite implements ActionListener{
  private Frame f;
  private Panel panWrite,panRead;
  private Button BtnRead,BtnWrite;
  private TextArea txtRead,txtWrite;

public static void main(String args[]) throws IOException{
   FileOutputStream outStream=new FileOutputStream("WorkShop.txt");
  // String s="546"; 
  //  for(int i=0;i<s.length();++i){
  //    outStream.write(s.charAt(i));
  //  }
  

   ReadWrite readwrite=new ReadWrite();
   readwrite.go();
}

public void go(){
    f=new Frame("Read & Write");
    f.setSize(450,300);

    panWrite=new Panel();
   // panWrite.setBackground(Color.red);
    txtWrite=new TextArea(7,50);
   // string=txtWrite.getText();

    BtnWrite=new Button("Write");
    BtnWrite.addActionListener(this);
    panWrite.add(txtWrite);
    panWrite.add(BtnWrite);

    panRead=new Panel();
    txtRead=new TextArea(7,50);

    BtnRead=new Button("Read");
    panRead.add(txtRead);
    panRead.add(BtnRead);

  f.add(panWrite,BorderLayout.NORTH);
  f.add(panRead,BorderLayout.CENTER);
  f.setVisible(true);

  
}
 
public void actionPerformed(ActionEvent e){  
   if (e.getSource()==BtnWrite){
          for(int i=0;i<txtWrite.getText().length();++i){
             outStream.write(txtWrite.getText().charAt(i));
      
     }
     
    }
   }


}

//ผมสร้างหน้าจอขึ้นมาโดยมี TextArea เพื่อรับค่าแล้วทำการบันทึกลงใน WordShop.txt นะครับ คือไม่สามารถบันทึกได้ครับเกิด Error ครับ ช่วยดูให้หน่อยนะครับ ถ้ามีเวลาขอช่วยเพิ่มเวลาคลิกที่ปุ่ม Read ให้นำข้อมูลที่เขียนมาแสดงใน TextArea ที่ได้สร้างเตรียมไว้นะครับ เพิ่งหัด นะครับ ช่วยหน่อยนะครับ

bit69
bit69
Thu 26 Jan 2006 11:48:36

ช่วยดูให้หน่อยนะครับ

ขอบคุณครับ

Administrator
Thu 26 Jan 2006 15:01:48
ลองดูที่นี่ครับ
http://www.javacoffeebreak.com/faq/faq0004.html
http://java.sun.com/docs/books/tutorial/essential/io/filestreams.html
เป็นตัวอย่างการใช้งาน IO FileStream ครับ
korakot
korakot
Tue 31 Jan 2006 18:54:04

รบกวนหน่อยนะครับ พอมี web ที่แจกโปรแกรมเล็ก ๆที่ทำจากภาษา จาว่า หรือป่าวครับ ช่วยแนะนำหน่อยครับ

Administrator
Tue 31 Jan 2006 22:16:43
ไม่แน่ใจว่าจะเล็กพอหรือไม่ลองเลือกดูแล้วกันครับ
http://sourceforge.net/search/?words=java&type_of_search=soft
korakot
korakot
Wed 1 Feb 2006 02:41:20

ขอบคุณมากครับ

bit69
bit69
Wed 1 Feb 2006 22:42:03

รบกวนช่วยเป็นแนวทางให้หน่อยนะครับ คือเวลาที่ผมทำการ INSERT INTO ลงฐานข้อมูล แล้วสามารถที่จะทำการ insert ได้นะครับ แต่ถ้าหากว่าผมจะรับค่ามาจาก JTextField แล้วทำการนำค่าจาก text มาบันทึกไม่สามารถทำได้ครับ งง มากเลยครับ ช่วยแก้ไขเป็นแนวทางให้ผมหน่อยนะครับ

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.JOptionPane;
import javax.swing.*;


public class FriendShip extends JFrame implements ActionListener {
  private JButton Button_moveFirst,Button_moveLast,Button_moveNext,
    Button_movePrevios,Button_Exit;
  private JButton Button_Add,Button_Edit,Button_Delete,Button_Find,Button_Process,
     Button_Cancel;
  private JLabel Label_Id,Label_Name,Label_Sex,Label_Birthday,Label_Age,Label_Faclty,
     Label_Address,Label_Title;
  private JLabel Label_pic=null;
  private JTextField TextField_Id,TextField_Name,TextField_Sex,TextField_Birthday,
     TextField_Age,TextField_Faculty;
  private JTextArea TextArea_Add=null;
  private JComboBox cbDate=null,cbMonth=null,cbYear=null,cbFaculty,cbSex=null;
  private JRadioButton raMale=null,raFemale=null;
     ButtonGroup groupSex=null;
  private ImageIcon pic=null;
  static ResultSet recRead=null,recExecute=null;
  static Connection con=null;
//  static ConnectDatabase Data=null;
  static boolean checkAddData=false,checkEditData=false;
  static private Container c=null;
  static private String sex="M";

  
    FriendShip(){
       super(":::FriendShip:::");
     setResizable(false);
     c=getContentPane();
     Font fontLabel=new Font("dialog",0,16); Font fontTextField=new Font("serif",0,20);
     c.setLayout(null);
     Button_Exit=new JButton("Exit");
     c.add(Button_Exit);
     Button_Exit.setBounds(540,480,90,30);
     Button_Exit.setFont(fontTextField);
     Button_Exit.addActionListener(this);
     
     Button_moveLast=new JButton(">|");
     Button_moveFirst=new JButton("|<");
       Button_moveNext=new JButton(">>");
       Button_movePrevios=new JButton("<<");

     //Button_moveNext.addActionListener(this);
    // Button_movePrevios.addActionListener(this);
    // Button_moveFirst.addActionListener(this);
    // Button_moveLast.addActionListener(this);


     Button_moveNext.setToolTipText("àÅ×è͹ä»Âѧ Record ¶Ñ´ä»");
     Button_movePrevios.setToolTipText("àÅ×è͹ä»Âѧ Record ¡è͹˹éÒ");
     Button_moveFirst.setToolTipText("àÅ×è͹ä»Âѧ Record áá");
     Button_moveLast.setToolTipText("àÅ×è͹ä»Âѧ Record ÊØ´·éÒÂ");
     
     
     c.add(Button_moveLast);
     c.add(Button_moveNext);
     c.add(Button_movePrevios);
     c.add(Button_moveFirst);
     
     Button_moveFirst.setBounds(10,410,90,30);
     Button_moveLast.setBounds(540,410,90,30);
     Button_moveNext.setBounds(430,410,90,30);
     Button_movePrevios.setBounds(120,410,90,30);
     
     
   //===========================================================
     Button_Add=new JButton("Add");
     Button_Edit=new JButton("Edit");
     Button_Delete=new JButton("Delete");
     Button_Find=new JButton("Find");
     Button_Process=new JButton("Process");
     Button_Cancel=new JButton("Cancel");

     Button_Add.addActionListener(this);
    // Button_Edit.addActionListener(this);
   //  Button_Delete.addActionListener(this);
   //  Button_Find.addActionListener(this);
    // Button_Process.addActionListener(this);
     Button_Cancel.addActionListener(this);

     c.add(Button_Add);
     c.add(Button_Edit);
     c.add(Button_Delete);
     c.add(Button_Find);
     c.add(Button_Process);
     c.add(Button_Cancel);

     Button_Add.setBounds(10,350,90,30);
     Button_Edit.setBounds(120,350,90,30);
     Button_Delete.setBounds(260,350,120,30);
     Button_Find.setBounds(540,350,90,30);
     Button_Process.setBounds(430,350,90,30);
     Button_Cancel.setBounds(260,410,120,30);
     //=============================================================
      Label_Title=new JLabel("FrinedShip",JLabel.RIGHT);
        Label_Id=new JLabel("ÃËÑʹѡÈÖ¡ÉÒ:",JLabel.RIGHT);
      Label_Name=new JLabel("ª×èÍ-Ê¡ØÅ:",JLabel.RIGHT);
      Label_Sex=new JLabel("à¾È:",JLabel.RIGHT);
      Label_Birthday=new JLabel("Çѹà¡Ô´:",JLabel.RIGHT);
      Label_Age=new JLabel("ÍÒÂØ:",JLabel.RIGHT);
      Label_Faclty=new JLabel("ÊÒ¢Ò:",JLabel.RIGHT);
      Label_Address=new JLabel("·ÕèÍÂÙè:",JLabel.RIGHT);

      Label_Id.setFont(fontLabel);
      Label_Name.setFont(fontLabel);
      Label_Sex.setFont(fontLabel);
      Label_Birthday.setFont(fontLabel);
      Label_Age.setFont(fontLabel);
      Label_Faclty.setFont(fontLabel);
      Label_Address.setFont(fontLabel);

      c.add(Label_Title);
      c.add(Label_Id);
      c.add(Label_Name);
      c.add(Label_Sex);
      c.add(Label_Birthday);
      c.add(Label_Age);
      c.add(Label_Faclty);
      c.add(Label_Address);

      Label_Title.setBounds(250,10,100,30);
      Label_Id.setBounds(110,45,100,30);
      Label_Name.setBounds(110,82,100,30);
      Label_Sex.setBounds(110,119,100,30);
      Label_Birthday.setBounds(110,156,100,30);
      Label_Age.setBounds(110,193,100,30);
      Label_Faclty.setBounds(110,230,100,30);
      Label_Address.setBounds(110,267,100,30);


      TextField_Id=new JTextField(10);
      TextField_Name=new JTextField();
      TextField_Sex=new JTextField();
      TextField_Birthday=new JTextField();
      TextField_Age=new JTextField(3);
      TextField_Faculty=new JTextField();
      TextArea_Add=new JTextArea(3,30);

      TextField_Id.setFont(fontTextField);
      TextField_Name.setFont(fontTextField);
      TextField_Sex.setFont(fontTextField);
      TextField_Birthday.setFont(fontTextField);
      TextField_Age.setFont(fontTextField);
      TextField_Faculty.setFont(fontTextField);
      TextArea_Add.setFont(fontTextField);

      TextField_Id.setFont(fontTextField);
      TextField_Name.setFont(fontTextField);
      TextField_Sex.setFont(fontTextField);
      TextField_Birthday.setFont(fontTextField);
      TextField_Age.setFont(fontTextField);
      TextField_Faculty.setFont(fontTextField);
      TextArea_Add.setFont(fontTextField);


      c.add(TextField_Id);
      c.add(TextField_Name);
      c.add(TextField_Sex);
      c.add(TextField_Birthday);
      c.add(TextField_Age);
      c.add(TextField_Faculty);
      c.add(TextArea_Add);

      TextField_Id.setBounds(220,50,105,25);
      TextField_Name.setBounds(220,85,160,25);
      TextField_Sex.setBounds(220,125,45,25);
      TextField_Birthday.setBounds(220,160,130,25);
      TextField_Age.setBounds(220,200,30,25);
      TextField_Faculty.setBounds(220,235,200,25);
      TextArea_Add.setBounds(220,270,350,65);

      //setEnableTextField(false,false,false,false,false,false,false);


      raMale=new JRadioButton("ªÒÂ",true);
      raFemale=new JRadioButton("Ë­Ô§",false);
      groupSex=new ButtonGroup();
      raMale.setFont(fontTextField);
      raFemale.setFont(fontTextField);
      groupSex.add(raMale);
      groupSex.add(raFemale);

      Vector vDate=new Vector();
      for(int i=1;i<32;i++){
        vDate.addElement(Integer.toString(i));
      }

      cbDate=new JComboBox(vDate);
      cbDate.setMaximumRowCount(5);
      cbDate.setToolTipText("àÅ×Í¡Çѹà¡Ô´");

      String[]month={"Á¡ÃÒ¤Á","¡ØÁÀҾѹ¸ì","ÁÕ¹Ò¤Á","àÁÉÒ¹","¾ÄÉÀÒ¤Á","ÁԶعÒ¹","¡Ã¡®Ò¤Á","ÊÔ§ËÒ¤Á","¡Ñ¹ÂÒ¹","µØÅÒ¤Á","¾ÄȨԡÒ¹","¸Ñ¹ÇÒ¤Á"};
      cbMonth=new JComboBox(month);
      cbMonth.setFont(fontTextField);
      cbMonth.setMaximumRowCount(5);
      cbMonth.setToolTipText("àÅ×Í¡à´×͹à¡Ô´");
      Vector vYear=new Vector();
         for(int i=2146;i<2550;i++){
          vYear.addElement(Integer.toString(i));
        }

      cbYear=new JComboBox(vYear);
      cbYear.setMaximumRowCount(5);
      cbYear.setToolTipText("àÅ×Í¡»Õ ¾.È. ·Õèà¡Ô´");

      //===========================Maejae=========================//

      String[]faculty={"à·¤â¹âÅÂÕÊÒÃʹà·È","ºÑ­ªÕ","¡ÒõÅÒ´","¡ÒèѴ¡ÒÃ","·Òͧà·ÕèÂÇàªÔ§¹ÔàÇÈ","à¡ÉµÃ»èÒäÁé"};
      cbFaculty=new JComboBox(faculty);
      cbFaculty.setFont(fontTextField);
      cbFaculty.setMaximumRowCount(5);
      cbFaculty.setToolTipText("àÅ×Í¡ÊÒ¢Ò·ÕèÊѧ¡Ñ´");

    //  raMale.addItemListener(this);
    //  raFemale.addItemListener(this);
    //  cbYear.addItemListener(this);
    String[]Sex={"ªÒÂ","Ë­Ô§"};
    cbSex=new JComboBox(Sex);
    cbSex.setFont(fontTextField);
    cbSex.setMaximumRowCount(5);


   // c.add(cbSex);
   // c.add(cbFaculty);
    //c.add(raMale);
   // c.add(raFemale);
    //c.add(cbDate);
   // c.add(cbMonth);
   // c.add(cbYear);

    //  cbSex.setBounds(220,125,80,25);
     // raMale.setBounds(220,125,50,25);
    //  raFemale.setBounds(270,125,50,25);
   //   cbDate.setBounds(220,160,50,25);
   //   cbMonth.setBounds(275,160,100,25);
   //   cbYear.setBounds(380,160,60,25);
   //   cbFaculty.setBounds(220,235,120,25);

     // setVisibleRadioButton(false,false);

      
   // JRadioButton raMale=null,raFemale=null;
    // ButtonGroup groupSex=null;

}


   public void actionPerformed(ActionEvent e){
     if(e.getSource()==Button_Cancel){
   TextField_Id.setText("");
   TextField_Name.setText("");
   TextField_Sex.setText("");
   TextField_Birthday.setText("");
   TextField_Age.setText("");
   TextField_Faculty.setText("");
   TextArea_Add.setText("");
   
   }

   if(e.getSource()==Button_Exit){
     System.exit(0);
   }
   if(e.getSource()==Button_Add){
     addData();
   }
  


  }     

 


   public static void main(String args[]){
      FriendShip f=new FriendShip();
 
    try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      String url="jdbcSurpriseddbc:FriendShipDB";
      Connection con=DriverManager.getConnection(url,"","");
      DatabaseMetaData meta=con.getMetaData();
      System.out.println(meta.getDatabaseProductName());
    }
    catch(Exception e){
      JOptionPane.showMessageDialog(null,e.getMessage(),"äÁèÁÕ°Ò¹¢éÍÁÙÅ",JOptionPane.ERROR_MESSAGE);
    }


    

    f.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
      System.exit(0);
      }
      });
      f.setSize(650,550);
      f.setLocation(80,50);
      f.setVisible(true);
      }

//  public static void readPic(String namePic){
//   if (pic==null && Label_pic==null ){
//   pic=new ImageIcon("./picture/"+namePic+".gif");
//   Label_pic=new JLabel(pic,JLabel.CENTER);
//   c.add(Label_pic);
//   Label_pic.setBounds(420,30,250,100);
// }  


public static void addData(){
   // String txtField_Id=TextField_Id.getText();
   String sql="INSERT INTO TblFriendShip(id,name,Sex,Brthday,old,Faclty,Address)"+
      "VALUES(?,?,?,?,?,?,?)";

    try{
       // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      String url="jdbcSurpriseddbc:FriendShipDB";
      Connection con=DriverManager.getConnection(url,"","");
      Statement stat=con.createStatement();
   //   stat.executeUpdate(sql);
          PreparedStatement pstmt=con.prepareStatement(sql);
      pstmt.setString(1,"002");
      pstmt.setString(2,"name");
      pstmt.setString(3,"sex");
      pstmt.setString(4,"birthday");
        pstmt.setString(5,"old");
      pstmt.setString(6,"Faclty");
        pstmt.setString(7,"Address");
     // pstmt.executeUpdate();
    //  pstmt.close();
        stat.close();
      con.close();
        JOptionPane.showMessageDialog(null,"ºÑ¹·Ö¡¢éÍÁÙÅàÃÕºÃéÍÂáÅéǤÃѺ","INFORMATION",JOptionPane.INFORMATION_MESSAGE); 


     
     }
    catch(SQLException e){
     JOptionPane.showMessageDialog(null,"Can not Update Data","Move Record",JOptionPane.ERROR_MESSAGE);
    System.out.println(e.getMessage());
    }

// JOptionPane.showMessageDialog(null,"test....","Move Record",JOptionPane.WARNING_MESSAGE);

             
    

 


 
 
  }

ขอบคุณครับ

Administrator
Wed 1 Feb 2006 23:06:52
public static void addData(){
   // String txtField_Id=TextField_Id.getText();
   String sql="INSERT INTO TblFriendShip(id,name,Sex,Brthday,old,Faclty,Address)"+
      "VALUES(?,?,?,?,?,?,?)";

    try{
       // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      String url="jdbcSurpriseddbc:FriendShipDB";
      Connection con=DriverManager.getConnection(url,"","");
      Statement stat=con.createStatement();
   //   stat.executeUpdate(sql);
          PreparedStatement pstmt=con.prepareStatement(sql);
      pstmt.setString(1,"002");
      pstmt.setString(2,"name");
      pstmt.setString(3,"sex");
      pstmt.setString(4,"birthday");
        pstmt.setString(5,"old");
      pstmt.setString(6,"Faclty");
        pstmt.setString(7,"Address");
     // pstmt.executeUpdate();
    //  pstmt.close();
        stat.close();
      con.close();
        JOptionPane.showMessageDialog(null,"ºÑ¹·Ö¡¢éÍÁÙÅàÃÕºÃéÍÂáÅéǤÃѺ","INFORMATION",JOptionPane.INFORMATION_MESSAGE); 


     
     }
    catch(SQLException e){
     JOptionPane.showMessageDialog(null,"Can not Update Data","Move Record",JOptionPane.ERROR_MESSAGE);
    System.out.println(e.getMessage());
    }

// JOptionPane.showMessageDialog(null,"test....","Move Record",JOptionPane.WARNING_MESSAGE);

}
เข้าใจว่าคุณได้ทำการเพิ่มข้อมูลที่เป็น static data อยู่นะครับ ยังไงลองอ่านข้อมูลจาก textField มาใส่ตัวแปรแล้วทำการ assign ให้กับ statement ดูครับ
bit69
bit69
Thu 2 Feb 2006 00:20:07

ผมลองเอาค่าใน textField มาใส่ในตัวแปรนะครับ

String txtField_Id=TextField_Id.getText();

ผมไม่รู้ทำไมถึง Error ครับ  คอมไพล์ไม่ผ่านซักทีครับ พอจะแนะแนวทางให้หน่อยได้หรือป่าวครับ

Administrator
Thu 2 Feb 2006 00:40:29
ไม่ทราบว่าที่ Compile ไม่ผ่านแจ้ง Error ใดครับ

ซึ่งเท่าที่ผมอ่านทั้งหมดดู ต้องขอแจ้งก่อนว่าผมไม่ได้ถนัดเรื่อง Java นะครับ
ถ้าเป็นในภาษา C แล้วการประกาศ static function หรือ method ใน Java นั้น ตัวแปรที่จะสามารถเข้าถึงได้นั้นต้องเป็น static member เหมือนกันนะครับ

ซึ่งถ้า Syntax ของ Java กับ C เหมือนกันนั้นที่คุณประกาศ
public static void addData() และมีการเรียกใช้ object TextField_Id ซึ่งไม่ได้ประกาศเป็น Static member ครับ

ผิดพลาดประการใดต้องขออภัยด้วยครับ
Reply
Name:
E-mail:
Home | Services | Forum | Classified | Directories | Support | Contact
ATOM feed RSS 0.9 feed RSS 1.0 feed RSS 2.0 feed
Copyright © 2005 - 2007 Modoeye.com, All Rights Reserved.
Disclaimer | Privacy policy | Term of Use | Term of Services
Valid XHTML Valid CSS! PHP: Hypertext Preprocessor MySQL database Apache Powered! FreeBSD Power to serve
Modoeye Sitemap Client login