anyone know what is wrong with my code? print("<% /* Create string of connection url within specified format with machine name, port number and database name. Here machine name id localhost and database name is student. */ String URL = "jdbc:oracle:thin:@10.2.160.244:1521:prodasdb"; // declare a connection by using Connection interface Connection conn = null; /* declare object of Statement interface that is used for executing sql statements. */ Statement stmt = null; // declare a resultset that uses as a table for output data from tha table. ResultSet rs = null; // Load JBBC driver "com.mysql.jdbc.Driver" Class.forName("oracle.jdbc.OracleDriver").newInstance(); /* Create a connection by using getConnection() method that takes parameters of string type connection url, user name and password to connect to database.*/ conn = DriverManager.getConnection(URL, "qascodvp", "qascodvp"); /* createStatement() is used for create statement object that is used for sending sql statements to the specified database. */ stmt = conn.createStatement(); String PID = request.getParameter("PID"); String MSG_TEXT = request.getParameter("msg_text"); PreparedStatement pstatement = null; int updateQuery = 0; // sql query to retrieve values from the specified table. String QueryString = "insert into xxqc_announcement(PID,msg_text) values (?,?)"; pstatement = conn.prepareCall(QueryString); pstatement.setString(1,PID); pstatement.setString(2,MSG_TEXT); updateQuery = pstatement.executeUpdate(); %> "); <form action="index.jsp" method="get"> <input style="width: 850px; " type="text" name="msg_text" /> <input style=" background-color: #999999; color: white" type="submit" value="Save" /> </form>
Here we have used MySQL database:
1)form.html:
<html> <form method="post" action="http://localhost:8080/examples/jsp/insert.jsp"> <table> <tr><td>First Name:</td><td><input type="text" name="fname"></td></tr> <tr><td>Last Name:</td><td><input type="text" name="lname"></td></tr> <tr><td>Email:</td><td><input type="text" name="email"></td></tr> <tr><td>Password:</td><td><input type="password" name="pass"></td></tr> <tr><td>Confirm Password:</td><td><input type="password" name="cpass"></td></tr> <tr><td>Date Of Birth</td><td><input type="text" name="dob"></td></tr> <tr><td>Age:</td><td><input type="text" name="age"></td></tr> <tr><td>Gender</td><td><input type="text" name="gender"></td></tr> <tr><td>Address:</td><td><input type="text" name="address"></td></tr> <tr><td>Country</td><td><input type="text" name="country"></td></tr> <tr><td>State:</td><td><input type="text" name="state"></td></tr> <tr><td>City</td><td><input type="text" name="city"></td></tr> <tr><td>Telephone No:</td><td><input type="text" name="tno"></td></tr> <tr><td>Mobile:</td><td><input type="text" name="mobile"></td></tr> <tr><td></td><td><input type="submit" value="Submit"></td></tr> </table> </form> </html>
2)insert.jsp:
<%@page import="java.sql.*,java.util.*"%> <% String fname=request.getParameter("fname"); String lname=request.getParameter("lname"); String email=request.getParameter("email"); String pass=request.getParameter("pass"); String cpass=request.getParameter("cpass"); String dob=request.getParameter("dob"); int age=Integer.parseInt(request.getParameter("age")); String gender=request.getParameter("gender"); String address=request.getParameter("address"); String country=request.getParameter("country"); String state=request.getParameter("state"); String city=request.getParameter("city"); int telephone=Integer.parseInt(request.getParameter("tno")); int mobile=Integer.parseInt(request.getParameter("mobile")); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia", "root", "root"); Statement st=con.createStatement(); int i=st.executeUpdate("insert into student(firstname,lastname,email,pass,confirm_pass,dob,age,gender,address,country,state,city,telephone,mobile) values('"+fname+"','"+lname+"','"+email+"','"+pass+"','"+cpass+"','"+dob+"',"+age+",'"+gender+"','"+address+"','"+country+"','"+state+"','"+city+"',"+telephone+","+mobile+")"); out.println("Data is successfully inserted!"); } catch(Exception e){ System.out.print(e); e.printStackTrace(); } %>
Ads