
its not working the code for databae access using servlets

Hello Friend,
In your previous post, you have specified that values are getting inserted but problem lies in retrieval. This means that your syntax for retrieving the values is incorrect. We are providing you a code, just go through it.
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DataInsertion extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String url = "jdbc:mysql://localhost/test?user=root&password=root";
Connection conn;
ResultSet rs;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url);
Statement statement = conn.createStatement();
String query = "insert into emp(EMP_NAME,SALARY) values('roseindia', 10000)";
int i = statement.executeUpdate(query);
if(i!=0){
out.println("The record has been inserted<br>");
}
else{
out.println("Sorry! Failure");
}
rs = statement.executeQuery("select * from emp");
while(rs.next()){
out.println(""+rs.getString("EMP_NAME") + " " + rs.getInt("SALARY") + "<br>");
}
rs.close();
statement.close();
}
catch (Exception e){
System.out.println(e);
}
}
}
In the above code, test is our database and root is our username and password. We have used the driver "com.mysql.jdbc.Driver" and our table is emp.
CREATE TABLE `emp` (
`EMP_NO` int(10) NOT NULL auto_increment,
`EMP_NAME` varchar(100) default NULL,
`SALARY` int(100) default NULL,
PRIMARY KEY (`EMP_NO`)
);
For more information, visit the following links:
Thanks

Web.xml:
<servlet>
<servlet-name>DataInsertion</servlet-name>
<servlet-class>DataInsertion</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DataInsertion</servlet-name>
<url-pattern>/DataInsertion</url-pattern>
</servlet-mapping>
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.