简体   繁体   中英

unclosed quote mark after the character string java

i am reading 3 pakets like mail to mail subject and mail message bu but when i am inserting data into database i got this error here is a somne part of code

aSocket.receive(to);
String towards=new String(to.getData());
System.out.println(towards);
aSocket.receive(sub);
String subject=new String(sub.getData());
aSocket.receive(msg);
String message=new String(msg.getData());
sendedmail(towards,subject,message,username);

public void sendedmail(String to,String sub,String msg,String from){
    try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }catch(ClassNotFoundException ex){
        JOptionPane.showMessageDialog(null,ex.getMessage());
    }
    try{
        Connection con=DriverManager.getConnection("jdbc:odbc:udp");
        Statement st=con.createStatement();
        int status=0;
        String query;
        query = "INSERT INTO emails(emailto, emailfrom, message, userstatus, subject) VALUES('"+to+"','"+from+"','"+msg+"','"+status+"','"+sub+"')";
        int f=st.executeUpdate(query); 
        if(f==1){
            System.out.println("row inserted");
        }
        else
        {
            System.out.println("row not inserted");
        }  
    }catch(SQLException ex){
        JOptionPane.showMessageDialog(null,ex.getMessage());
    }

In SQL, quotes are escaped by doubling them, eg:

insert ... values ('O''Brien');

So you need to replace all quotes with doubled quotes:

 str = str.replace("'", "''");

Oh, and it's been about 15 years since you needed to load the JDBC driver class before using it; just delete the whole first try-catch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM