|
 |
pro_java_server thread: Connecting database using jsp
Message #1 by bishwa basu <bishwa_bose@y...> on Mon, 9 Apr 2001 00:06:22 -0700 (PDT)
|
|
Hi,
I found this code with this arcticle.
http://softwaredev.earthweb.com/devtools/article/0,,12061_626511,00.html
//------------------------------------------------------
query.jsp
<html>
<body>
This is the Query page.
<p>
<form method=post action="query2.jsp">
Name: <input type=text name=tname>
<br>
<input type=submit>
</form>
</body>
</html>
//------------------------------------------------------
query2.jsp
<html>
<body>
<%@ page language="java" %>
<jsp:useBean id="queryBean" scope="request" class="QueryBean1" />
<%
queryBean.setName(request.getParameter("tname"));
%>
The result of the query is:
<table>
<tr>
<th> Noms </th> <th> Prénoms </th> <th> Adresses </th><th> Téléphones
</th><th> Notes </th>
</tr>
<%= queryBean.doQuery() %>
</table>
<p>
</body>
</html>
//------------------------------------------------------
queryBean.java
package DB;
import java.sql.*;
import java.io.*;
public class QueryBean {
// private String dbURL = "jdbc:odbc:Contacts4";
private String dbURL = "jdbc:mysql://localhost/test";
private String dbDriver = "org.gjt.mm.mysql.Driver";
private Connection dbCon;
String Name = null;
public QueryBean() {
super();
}
public String getName() {
return this.Name;
}
public void setName(String tname) {
this.Name = tname;
}
public String doQuery() throws ClassNotFoundException, SQLException {
// Class.forName(dbDriver);
Class.forName(dbDriver);
dbCon = DriverManager.getConnection(dbURL);
Statement s = dbCon.createStatement();
String sql = "SELECT * FROM Adresses WHERE Last_Name like '" +
this.Name
+ "%' or Notes like '" +"%"+ this.Name
+ "%' or Company_Name like '" +"%"+ this.Name + "%'";
//System.out.println("sql : "+sql);
ResultSet rs = s.executeQuery(sql);
String rt = "";
while (rs.next()) {
rt = rt + "<tr> <td>" + rs.getString("Last_Name") + "</td>";
rt = rt + "<td>" + rs.getString("First_Name") + "</td>";
rt = rt + "<td>" + rs.getString("Address") + "</td>";
rt = rt + "<td>" + rs.getString("Phone") + "</td>";
rt = rt + "<td>" + rs.getString("Type");
rt = rt + "<td>" + " -> "+rs.getString("Notes") + "</td> </tr>";
}
dbCon.close();
return rt;
}
}
srikanth c wrote:
> Hi I understand your problem,
> write a jsp file in which u use the connection object
> using the jdbcodbc driver to get the connection.
> which will return Connection Object.
> which u can use in the other jsp files.
>
> Bye
>
> --- bishwa basu <bishwa_bose@y...> wrote:
> > HI
> >
> > I am very new in jsp. At present I am trying to
> > connect database using jsp which in turn use
> > bean(ie.
> > package) to connect databse to insert data into the
> > database and to display the data. Here I am using MS
> > Access database and using jdbc odbc driver.
> >
>
|
|
 |