主题:[讨论]JSP做通讯录与SQL连接不上????
package phone.com;
import java.sql.*;
public class phone {
private String driver = "sun.jdbc.odbc.JdbcOdbc";
private String url = "jdbc:odbc:javab";
private Connection con = null;
private Statement state = null;
private ResultSet rs = null;
public phone() {
open();
}
public boolean open() {
try {
Class.forName(driver);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
return false;
}
try {
con = DriverManager.getConnection(url);
state = con.createStatement();
} catch (SQLException ex1) {
ex1.printStackTrace();
return false;
}
return true;
}
public void close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (state != null) {
state.close();
state = null;
}
if (con != null && !con.isClosed()) {
con.close();
con = null;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public int update(String sql) throws SQLException {
return state.executeUpdate(sql);
}
public ResultSet selectAll(String sql) {
try {
return state.executeQuery(sql);
} catch (SQLException ex) {
return null;
}
}
public boolean deleteid(String sql){
try {
return state.execute(sql);
} catch (SQLException ex) {
ex.printStackTrace();
return false;
}
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//add.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
add
</title>
</head>
<body bgcolor="#ffffff">
<h1 align="center">
添加通讯信息
</h1>
<form method="post" action="addheadler.jsp">
<table align="center">
<tr>
<td colspan="1"><div>名字</div><div><input type="text" name="txtname"></div></td>
<td colspan="1"><div>电话</div><div><input type="text" name="txtphone"></td>
</tr>
</table>
<hr />
<table align="center">
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
</table>
</form>
</body>
</html>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//addheadler.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="phone.com.*" %>
<%
try {
String name=request.getParameter("txtname");
String phone=request.getParameter("txtphone");
phone ph = new phone();
String sql = "update db set name='"+name+"',phone='"+phone+"'";
ph.update(sql);
response.sendRedirect("add.jsp");
ph.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
%>
add.jsp提交到addhealer.jsp不显示add.jsp。。添加数据功能也没能实现。
import java.sql.*;
public class phone {
private String driver = "sun.jdbc.odbc.JdbcOdbc";
private String url = "jdbc:odbc:javab";
private Connection con = null;
private Statement state = null;
private ResultSet rs = null;
public phone() {
open();
}
public boolean open() {
try {
Class.forName(driver);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
return false;
}
try {
con = DriverManager.getConnection(url);
state = con.createStatement();
} catch (SQLException ex1) {
ex1.printStackTrace();
return false;
}
return true;
}
public void close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (state != null) {
state.close();
state = null;
}
if (con != null && !con.isClosed()) {
con.close();
con = null;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public int update(String sql) throws SQLException {
return state.executeUpdate(sql);
}
public ResultSet selectAll(String sql) {
try {
return state.executeQuery(sql);
} catch (SQLException ex) {
return null;
}
}
public boolean deleteid(String sql){
try {
return state.execute(sql);
} catch (SQLException ex) {
ex.printStackTrace();
return false;
}
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//add.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
add
</title>
</head>
<body bgcolor="#ffffff">
<h1 align="center">
添加通讯信息
</h1>
<form method="post" action="addheadler.jsp">
<table align="center">
<tr>
<td colspan="1"><div>名字</div><div><input type="text" name="txtname"></div></td>
<td colspan="1"><div>电话</div><div><input type="text" name="txtphone"></td>
</tr>
</table>
<hr />
<table align="center">
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
</table>
</form>
</body>
</html>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//addheadler.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="phone.com.*" %>
<%
try {
String name=request.getParameter("txtname");
String phone=request.getParameter("txtphone");
phone ph = new phone();
String sql = "update db set name='"+name+"',phone='"+phone+"'";
ph.update(sql);
response.sendRedirect("add.jsp");
ph.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
%>
add.jsp提交到addhealer.jsp不显示add.jsp。。添加数据功能也没能实现。