Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

How can I replace the single backslashes "\"in the string filepath with either double backslashes "\" or a single slash "/" ? I need it so that i can execute the desktop method in java properly

here is the code:

    JButton btnAdd = new JButton("Add");
    btnAdd.setBounds(101, 560, 89, 23);
    frmAddContract.getContentPane().add(btnAdd);
            btnAdd.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {

            try{

                String cntr = ContractNo.getText();
                String en = EngagerName.getText();
                String cont = contNo.getText();
                String mo = month.getText();
                String d = day.getText();
                String yr = year.getText();
                String dte = yr + "-" + mo + "-" + d;
                String cla = cladd.getText();
                String tm = tme.getText() + ":00";
                String evadd = eventadd.getText();
                String filepath = textField_fp.getText();
                String ref = refer.getText();





                String SQL = "insert into cis "+
                    "values ('"+cntr+"','"+en+"','"+
                    cont+"','"+cla+"','"+dte+"','"+tm+"','"+
                    evadd+"','"+filepath+"','"+ref+"');";
                PreparedStatement stmt = conn.prepareStatement(SQL);
                stmt.executeUpdate();
                JOptionPane.showMessageDialog(null, "Addition of Contract Successful!");
                frmAddContract.setVisible(false);
                PreparedStatement show = conn.prepareStatement("select * from kusinanikambal.cis;");
                ResultSet rs = show.executeQuery();
                System.out.println(rs.getRowId("engager"));


            }
            catch(Exception e){
                JOptionPane.showMessageDialog(null, e);
            }
                }


            });
share|improve this question
1  
This is way, way too much code; post the simplest version that shows what you're needing to do. –  chrylis Jan 21 '14 at 15:15
    
possible duplicate of How to replace " \ " with " \\ " in java –  JasonMArcher Jan 21 '14 at 18:07

1 Answer 1

up vote 0 down vote accepted
String filepath = textField_fp.getText().replaceAll("\\\\","\\\\\\\\");
share|improve this answer
    
it shows a java.util.regex.patternSyntaxException: Unexpected internal error near index 1 \ –  user3209957 Jan 21 '14 at 15:26
    
@user3209957 Sorry, forgot it has to be 2^escaped. –  chrylis Jan 21 '14 at 15:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.