Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

Fresher Job


 

Search Host

Monthly Fee($)
Disk Space (MB)
Register With us for Newsletter!
Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Have tutorials?
Add your tutorial to our Java Resource and get tons of hits.

We offer free hosting for your tutorials. and exposure for thousands of readers. drop a mail
roseindia_net@yahoo.com
 
   

Tutorials

Java Server Pages

JAXB

Java Beans

JDBC

MySQL

Java Servlets

Struts

Bioinformatics

Java Code Examples

Interview Questions

 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Web Promotion

Web Submission

Submit Sites

Manual Submission?

Web Promotion Guide

Hosting Companies

Web Hosting Guide

Web Hosting

Linux

Beginner Guide to Linux Server

Frameworks

Persistence Framework

Web Frameworks

Free EAI Tools

Web Servers

Aspect Oriented Programming

Free Proxy Servers

Softwares

Adware & Spyware Remover

Open Source Softwares

insertion in SQL
Expert:vasu
Query is "insert into employee values('"+eno+"','"+ename+"');"
in the ename place we will get employee names such as

John's Peter,Peter's Johnson etc....
i am not able to insert this kind of names in the database because of single code in the name. dbase is MS-SQL emp.name data type varchar(MAX).
why I am not able to insert single codes in the database. As far as I know string parameter in SQL is denoted as 'john' . I think thats why it is not accepting any single code..
any way..
Thanks for fast reply....
Pls. calrify my doubt........
Answers
Hi friend,


Code to solve the problem :


import java.io.*;
import java.sql.*;

public class InsertRecords{
public static void main(String[] args) {
System.out.println("Insert records example using prepared statement!");
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdetails","root","root";);
try{
String str = "John's";
str = str.replace("'","\\'");

String sql = "INSERT employee (username)VALUES('"+str+"')";

System.out.println(sql + sql);

PreparedStatement prest = con.prepareStatement(sql);

int count = prest.executeUpdate();
System.out.println(count + "row(s) affected");
con.close();
}
catch (SQLException s){
System.out.println(s);
}
}
catch (Exception e){
e.printStackTrace();
}
}
}

Thanks

Hi,

In java also we can't use \ directly.. as we use \\ to get printed. So,

For SQL queires we have to give two single quotes like
My name is John's Peter ---> My name is John''s Peter....

So in your java class it self before executing the query add the following line.

ename= ename.replaceAll("'","''");



More Questions
Post Answers
 
Ask Question Facing Programming Problem?
Useful Links
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.