I have created a simple jsp webpage that uses an sql server to return columns of info. NOw instead of accessing the sql DB directly from the JSP page I need to create a JAva file to attach to the jsp page that does the same thing. (the jsp code will be deleted and the Java file will be imported to the jsp page) Does that make sense? I have the code already for the jsp page.
Here is the working JSP code that displays all the columns I need:
<%@page
language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%>
<html>
<head>
<title>Connect to Northwind_64 Bit SQL Server DSN</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%
//setup url (bridge-driver name + ODBC DSN), user, and password Strings
String dsn = "Northwind_64";
String dbUrl = "jdbc:odbc:" + dsn;
String user = "";
String password = "";
Connection connection = null;
// try to load the JDBC-ODBC driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
out.print("About to try to connect to ODBC DSN: " + dbUrl + "<br /><br />");
//try to connect to database using the above Strings
connection = DriverManager.getConnection(dbUrl, user, password);
out.print("Connected to ODBC DSN: " + dbUrl + "<br /><br />");
Statement stmt;
System.out.println("Creating statement...");
stmt = connection.createStatement();
String sql;
sql = "SELECT ProductID, ProductName, UnitPrice FROM Northwind.dbo.Products";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("ProductID");
String is = rs.getString("ProductName");
double last = rs.getDouble("UnitPrice");
// Display values;
out.print("ProductID: " + id);
out.print(", UnitePrice: " + is);
out.print(", ProductName: " + last);
out.println("<br/>");
}
%>
</body>
</html>