2 hannah hannah hannah_hannah 于 2014.05.21 08:47 提问

如何将数据存储到数据库?

我们要做的事json编辑器,已经解析好了,可是每次上传都失败,出现java.net.SocketException: Connection reset报错。求指教
package com.lzw;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.net.*;

import javax.swing.*;
import javax.swing.border.BevelBorder;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import server.ConnectMySql;
import tree.*;

public class myclient extends JFrame implements ActionListener,MouseListener
{

JToolBar jt2;
JButton jb1_1,jb1_2,jb1_3,jb1_4,jb1_5,jb1_6,jb2_1,jb2_2,jb2_3,jb2_4,jb2_5,jb2_6,jb2_7,jb2_8,jb2_9,jb2_10,jb2_11;
JTextArea jta1,jTextArea2,jta3;
JSplitPane jsp1,jsp2,jsp3;
JScrollPane js1,js2,js3;
  JPanel panel1=new JPanel(new BorderLayout());
  JPanel panel2=new JPanel(new BorderLayout());
  JPanel panel3=new JPanel(new BorderLayout());
  JPanel panel4=new JPanel(new BorderLayout());
  JPanel panel5=new JPanel(new BorderLayout());
  JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP); 
  JMenuBar jmb;
  JMenu jm1,jm2,jm3,jm4;
  JMenuItem jm1_1,jm1_2,jm1_3,jm1_4,jm1_5,jm1_6,jm2_1,jm2_2,jm2_3,jm2_4,jm2_5,jm3_1 ,jm3_2,jm4_1;
  JSeparator jsp;
  JPanel pic;
  String nowfile;
  String text1= "",text2="",text3="";

private PrintWriter writer;
Socket socket;
private JTextArea ta = new JTextArea();
private JTextArea jta = new JTextArea();





Container cc;
public myclient(String title){
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    cc = this.getContentPane();
    final JScrollPane scrollPane = new JScrollPane();


    Container cp=getContentPane();  
    cp.setLayout(new BorderLayout());  
    final JButton jb = new JButton("发出去!!"); 
    JPanel panela=new JPanel();
    panela.add(jb);



    JPanel panel0=new JPanel();
    panel0.add(jta);
    scrollPane.setBorder(new BevelBorder(BevelBorder.RAISED));
    getContentPane().add(scrollPane,BorderLayout.CENTER);
    scrollPane.setViewportView(ta);

    jb.setSize(getSize());
    cc.add(jta,"South");
    cc.add(panela,"South");

    jb.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            writer.println(jta.getText());
            ta.append(jta.getText()+'\n');
            ta.setSelectionEnd(ta.getText().length());
            jta.setText("");}


    });
}
       public  void panel(){ //设置组件 

            jmb=new JMenuBar();
            jm1=new JMenu("文件");
            jm2=new JMenu("编辑");
            jm3=new JMenu("查看");
            jm4=new JMenu("帮助");

            jm1_1 = new JMenuItem();
            jm1.add(jm1_1);
            jm1_1.setText("新建文件夹");
            jm1_1.addActionListener(this);

            jm1_2 = new JMenuItem();
            jm1.add(jm1_2);
            jm1_2.setText("删除文件夹");
            jm1_2.addActionListener(this);

            jm1_3 = new JMenuItem();
            jm1.add(jm1_3);
            jm1_3.setText("新建文件");
            jm1_3.addActionListener(this);

            jm1_4 = new JMenuItem();
            jm1.add(jm1_4);
            jm1_4.setText("删除文件");
            jm1_4.addActionListener(this);

            jm1_5 = new JMenuItem();
            jm1.add(jm1_5);
            jm1_5.setText("文件改名");
            jm1_5.addActionListener(this);
            jsp = new JSeparator();
            jm1.add(jsp);
            jm1_6 = new JMenuItem();
            jm1.add(jm1_6);
            jm1_6.setText("退出");
            jm1_6.addActionListener(this);

            jm2_1 = new JMenuItem();
            jm2.add(jm2_1);
            jm2_1.setText("保存");
            jm2_1.addActionListener(this);

            jm2_2 = new JMenuItem();
            jm2.add(jm2_2);
            jm2_2.setText("复制");
            jm2_2.addActionListener(this);

            jm2_3 = new JMenuItem();
            jm2.add(jm2_3);
            jm2_3.setText("粘贴");
            jm2_3.addActionListener(this);

            jm2_4 = new JMenuItem();
            jm2.add(jm2_4);
            jm2_4.setText("剪切");
            jm2_4.addActionListener(this);

            jm2_5 = new JMenuItem();
            jm2.add(jm2_5);
            jm2_5.setText("解析json");
            jm2_5.addActionListener(this);

            jm3_1 = new JMenuItem();
            jm3.add(jm3_1);
            jm3_1.setText("刷新");
            jm3_1.addActionListener(this);

            jm3_2 = new JMenuItem();
            jm3.add(jm3_2);
            jm3_2.setText("登录记录");
            jm3_2.addActionListener(this);

            jm4_1 = new JMenuItem();
            jm4.add(jm4_1);
            jm4_1.setText("帮助文档");
            jm4_1.addActionListener(this);

            jmb.add(jm1);
            jmb.add(jm2);
            jmb.add(jm3);
            jmb.add(jm4);
            JPanel panel1=new JPanel(new BorderLayout());
            JPanel panel2=new JPanel(new BorderLayout());
            JPanel panel3=new JPanel(new BorderLayout());
            JPanel panel4=new JPanel(new BorderLayout());
            JPanel panel5=new JPanel(new BorderLayout());
            JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP); 
            setJMenuBar(jmb);


            //添加JToolBar组件
            jt2 = new JToolBar();
            panel1.add(jt2, BorderLayout.NORTH);
            //jt2.setPreferredSize(new Dimension(50, 40));


                jb2_2 = new JButton(new ImageIcon("images//Open.jpg"));                     
                jb2_2.setToolTipText("新建文件夹");
                jb2_2.addActionListener(this);
                jt2.add(jb2_2);
                jb2_1 = new JButton(new ImageIcon("images//Close.jpg"));
                jb2_1.setToolTipText("删除文件夹");
                jb2_1.addActionListener((this));
                jt2.add(jb2_1);
                jb2_3 = new JButton(new ImageIcon("images//New.jpg"));
                jb2_3.setToolTipText("新建文件");
                jb2_3.addActionListener(this);
                jt2.add(jb2_3);
                jb2_4 = new JButton(new ImageIcon("images//Break.jpg"));
                jb2_4.setToolTipText("删除文件");
                jb2_4.addActionListener(this);
                jt2.add(jb2_4);
                jb2_5 = new JButton(new ImageIcon("images//Check.jpg"));
                jb2_5.setToolTipText("文件改名");
                jb2_5.addActionListener(this);
                jt2.add(jb2_5);
                jb2_6 = new JButton(new ImageIcon("images//Save.jpg"));
                jb2_6.setToolTipText("保存");
                jb2_6.addActionListener(this);
                jt2.add(jb2_6);
                jb2_7 = new JButton(new ImageIcon("images//Copy.jpg"));
                jb2_7.setToolTipText("复制");
                jb2_7.addActionListener(this);
                jt2.add(jb2_7);
                jb2_8= new JButton(new ImageIcon("images//Paste.jpg"));
                jb2_8.setToolTipText("粘贴");
                jb2_8.addActionListener(this);
                jt2.add(jb2_8);
                jb2_9 = new JButton(new ImageIcon("images//Cut.jpg"));
                jb2_9.setToolTipText("剪切");
                jb2_9.addActionListener(this);
                jt2.add(jb2_9);

                jb2_11 = new JButton(new ImageIcon("images//About.jpg"));
                jb2_11.setToolTipText("解析");
                jb2_11.addActionListener(this);
                jt2.add(jb2_11);
                jb2_10 = new JButton(new ImageIcon("images//flush.jpg"));
                jb2_10.setToolTipText("刷新");
                jb2_10.addActionListener(this);
                jt2.add(jb2_10);
            panel1.add(panel2,BorderLayout.CENTER);
            jta1=new JTextArea();
            jTextArea2=new JTextArea();jTextArea2.setEditable(false);
            jta3=new JTextArea();jta3.setEditable(false);
            js1=new JScrollPane(jta1);
            js2=new JScrollPane(jTextArea2);
            js3=new JScrollPane(jta3);
            panel4.add(js1);
            panel5.add(js2);
            panel3.setLayout(new GridLayout(1,2));
            panel3.add(panel4);
            panel3.add(panel5);
            panel2.add(js3);
            jsp1=new JSplitPane(JSplitPane.VERTICAL_SPLIT,panel3,panel2);
            jsp1.setDividerLocation(500);
            panel1.add(jsp1);
       }






//利用递归方法解析json

        private void parseJsonArray(JSONArray ja, int depth) {
            for (int i = 0; i < ja.size(); i ++)
            {
                String strRawJson = ja.opt(i).toString();    //得到json文本
                int num = 0;
                char cTestChar = (strRawJson.charAt(num));    //转换成char

                switch (cTestChar)
                {
                case '{':
                    for (int xxx = 0; xxx < depth; xxx++) jTextArea2.append("\t");   //读取到“{”时
                    jTextArea2.append("[" + i + "]" + "  " + "Object"+"\n");   //格式化打印在文本框
                    //System.out.println("[" + i + "]" + " value is " + "Object");
                    parseJsonObject(ja.optJSONObject(i), depth+1);     //调用方法 
                    break;
                case '[':
                    for (int xxx = 0; xxx < depth; xxx++) jTextArea2.append("\t"); //读取到“[”时
                    jTextArea2.append("[" + i + "]" + "  " + "Array"+"\n");   //格式化打印在文本框
                    //System.out.println("[" + i + "]" + " value is " + "Array");                
                    parseJsonArray(ja.optJSONArray(i), depth+1);  //调用方法 
                    break;
                default:
                    for (int xxx = 0; xxx < depth; xxx++) jTextArea2.append("\t"); //未读取到括号时
                    jTextArea2.append("[" + i + "]" + "  " + strRawJson+"\n");  //格式化打印在文本框
                    break;
                    }
            }
        }


        //利用递归方法解析json
        private void parseJsonObject(JSONObject jo, int depth) {
            JSONArray ja = jo.names();   //得到json名
            for (int i = 0; i < ja.size(); i ++)
            {
                String strKey = ja.optString(i);   //得到json文本
                String strRawJson = jo.opt(strKey).toString();  
                char cTestChar = (strRawJson.charAt(0));    //转换成char
                switch (cTestChar){
                case '{':
                    for (int xxx = 0; xxx < depth; xxx++) jTextArea2.append("\t");   //读取到“{”时
                   // System.out.println(strKey + "   " + "Object");
                    jTextArea2.append(strKey+"  "+ "\tObject"+"\n");  //格式化打印在文本框
                    parseJsonObject(jo.optJSONObject(strKey), depth+1);  //调用方法 
                    break;
                case '[':
                    for (int xxx = 0; xxx < depth; xxx++) jTextArea2.append("\t");;   //读取到“[”时
                   // System.out.println(strKey + " value is " + "Array");
                    jTextArea2.append(strKey +"  "+"\tArray"+"\n");    //格式化打印在文本框
                    parseJsonArray(jo.optJSONArray(strKey), depth+1);  //调用方法 
                    break;
                default:
                    for (int xxx = 0; xxx < depth; xxx++) jTextArea2.append("\t");   //未读取到括号时
                  //  System.out.println(strKey + " value is " + strRawJson);
                    jTextArea2.append(strKey+"  "+strRawJson+"\n");   //格式化打印在文本框
                    break;
                }
            }
        }

        //实际调用解析Json的方法
        public void cutjson(){ 

            //JInternalFrame thisframe = this.getDesk().getSelectedFrame();
            //JTextArea thisPane = hm1.get(thisframe);
             String strJson = jta1.getText();    //得到文本区内容
              JSONObject root = null;        //定义json对象
               try {
                        root = JSONObject.fromObject(strJson);  //初始化json对象
                         } catch (Exception e1) {  //捕获异常
                                JOptionPane.showMessageDialog(this,e1.getMessage());  
                                jta3.setText(e1.getMessage());
                                    // e1.printStackTrace();
                                 }
                if (root == null)   //判断是否为空
                         return ;
                  parseJsonObject(root, 0);   //调用解析方法

           }

        //添加监听事件
        @Override
        public void actionPerformed(ActionEvent e) 
        {
          Object str = e.getSource();

          if (str == jb2_10 || str ==jm3_1)
          { 
              new Panel();
            this.dispose();
          }
          else if (str == jb2_2 || str ==jm1_1){//新建
              new Newfolder();
           }
          else if(str == jb2_1 || str ==jm1_2)
          {  new Deletfolder();

          }
        else if(str == jb2_3 || str ==jm1_3)
          {  new Newfile();

          }
        else if(str == jb2_4 || str ==jm1_4)
          {  new Deletefile();

          }
        else if(str == jb2_5 || str ==jm1_5)
          {  new Renamefile();

          }
        else if( str ==jm1_6)
      { this.dispose();

      }
        else if(str == jb2_6 || str ==jm2_1)
        {//保存
            if(nowfile!=null)
            { save(nowfile, jta1.getText());

            }

        }
        else if(str == jb2_7 || str ==jm2_2)
        {//复制
            jta1.copy();

        }
        else if(str == jb2_8 || str ==jm2_3)
        {//粘贴
            jta1.paste();
        }
        else if(str == jb2_9 || str ==jm2_4)
        {//剪切
            jta1.cut();
        }
        else if(str == jb2_11 || str ==jm2_5)
        {  jTextArea2.setText("");
          cutjson();

        }


        }


      //保存json文件,通过服务器传输
        public void save(String file,String text) {
            try {
                Socket st=new Socket();
                st.connect(new InetSocketAddress("127.0.0.1",2345));
                OutputStream os;
                os = st.getOutputStream();
                DataOutputStream dos = new DataOutputStream(os);
                InputStream in = st.getInputStream();
                DataInputStream dis = new DataInputStream(in);
                dos.writeUTF(file);
                dos.writeUTF(text);
                dos.writeUTF("保存");
                System.out.println("保存");
                dos.flush();
                Thread.sleep(1000);                     
                dis.close();
                dos.close();
                os.close();
                st.close();

            }catch(UnknownHostException e1) {
                e1.printStackTrace();
            }catch(IOException e2) {
                e2.printStackTrace();
            }catch(InterruptedException e3) {
                e3.printStackTrace();
            }
    }


private void connect(){
    ta.append("我正在努力链接\n");
    try{
        socket = new Socket("127.0.0.1",2345);
        writer = new PrintWriter(socket.getOutputStream(),true);
        ta.append("哈哈!我连上啦!\n");
        ta.append("输入吧!");
    }catch(Exception e){
        e.printStackTrace();


    }
}

@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    myclient client = new myclient("我的JSON编辑器");


    client.setSize(800,500);
    client.setVisible(true);
    client.panel();
    client.connect();
}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!