Create Dynamic project: File > New > Project....> Web > Dynamic Web Project.
Create Servlet class with name SampleTest.java(it will automatically makes entry in web.xml)
Paste the following code into doPost()
System.out.println("Selected value is : "+(String)request.getParameter("gameName"));
String gameName = (String)request.getParameter("gameName");
DBConnection dbConnection = new DBConnection();
dbConnection.insertQuery(gameName);
System.out.println("Query insert successfully");
Create DBConnection.java file
static Connection l_objConnection;
public static Connection getConnetion() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
// Your url -jdbc:oracle:thin:@localhost:1521:DBName
// your username - Test
// your password - Test
l_objConnection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:DBName", "username",
"password");
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block test
e.printStackTrace();
}
return l_objConnection;
}
public void insertQuery(String gameName) {
try {
// Replace your table name instead of TABLENAME
// Replace GAMENAME into colounm name.
PreparedStatement preStatement = l_objConnection
.prepareStatement("INSERT INTO TABLENAME(GAMENAME) VALUES (?)");
preStatement.setString(1, gameName);
preStatement.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Create index.jsp under WebContent folder.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<SCRIPT LANGUAGE="JavaScript">
<!--
function button1()
{
var value = document.getElementById("gameName").value;
alert(value);
document.SampleTest.submit()
}
// -->
</SCRIPT>
<body>
<FORM NAME="SampleTest" METHOD="POST" action="./SampleTest">
<select id="gameName" name="gameName">
<option>Sports</option>
<option>Corporate</option>
<option>Religious</option>
<option>Music</option>
</select>
<INPUT TYPE="submit" VALUE="Save" ONCLICK="button1()">
</FORM>
</body>
</html>
Web.xml
<servlet>
<description></description>
<display-name>SampleTest</display-name>
<servlet-name>SampleTest</servlet-name>
<servlet-class>com.test.SampleTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleTest</servlet-name>
<url-pattern>/SampleTest</url-pattern>
</servlet-mapping>
Output: