先说明一下,登陆流程,首先入口为login.html。然后为进入connnet.jsp,在这jsp中调用javabean(connectDB)主要完成和数据库的连接,把登陆的用户名和密码记录到user数据库里面。但我这没有写完善,其中还的完善检测功能,和一些具体的个人信息。在viewUser.jsp中,可以调用数据库中已经注册人的信息,在这里,我们可以加一些改动。
login.html
<html>
<title>
登陆页面
</title>
<body>
<div align=center>
<form action="connect.jsp" method="get">
<table border=3 cellspacing=3>
<tr><td>用户名:</td><td><input type="text" name="name"></td></tr><br>
<tr><td>密 码: </td><td><input type="password" name="password"></td></tr><br>
</table>
<input type="submit" value="提交">
</form>
</div>
</table>
</body>
</html>
connet.jsp
<%@page contentType="text/html;charset=gbk" language="java" import="java.sql.*"%>
<!--获得数据-->
<html>
<body>
<jsp:useBean id="connect" scope="page" class="xu.ConnectDB" />
<%
String name=request.getParameter("name");
String password=request.getParameter("password");
connect.AddUser(name,password);
%>
<a href="/Login.html">我要加人</a>
<a href="/viewUser2.jsp">看看有多少了啊?</a>
</body>
</html>
viewUser.jsp
<%@page contentType="text/html;charset=gb2312" language="java" import="java.sql.*,java.io.*" %>
<html>
<body>
<a href="/Login.html">我还要加</a>
<!--连接数据库,提取数据-->
<%="显示数据"%>
<%
try
{
int count=0;
Class.forName("org.gjt.mm.mysql.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/message","root","");
Statement stm=con.createStatement();
ResultSet rst=stm.executeQuery("select * from user");
while(rst.next())
{
out.println(rst.getString("name"));
out.println(rst.getString("password")+"<br>");
count++;
out.println("这是第"+ count +"条");
}
rst.close();
stm.close();
con.close();
}
catch(Exception e)
{
}
%>
</body>
</html>
connectDB.java
package xu;
import java.sql.*;
public class ConnectDB {
private Connection con;
public static Connection connectDB() {
Connection conn = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/message";
String username = "root";
String password = "";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public void AddUser(String name, String password) {
this.con = ConnectDB.connectDB();
try {
PreparedStatement stm = con
.prepareStatement("insert into user values(?,?)");
stm.setString(1, name);
stm.setString(2, password);
stm.execute();
stm.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getUser()
{
this.con=ConnectDB.connectDB();
try
{
Statement stm=con.createStatement();
String sql="select * from user";
ResultSet rst=stm.executeQuery(sql);
while(rst.next())
{
String name= rst.getString("name");
String password= rst.getString("password");
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
其基本情况就是这样,在设计connectDB.java时,我先用的一个不能在页面连接数据库!在北京的冯涛的指导下,完成了设计!谢谢啦!
下面为我以前的设计类,单独运行类没问题,但在设计页面时,连接数据库有问题?其具体晚上出了问题,我还在研究!
private Connection con;
public ConnectDB()
{
try
{
Class c1=Class.forName("org.gjt.mm.mysql.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/message","root","");
}
catch(Exception e)
{
}
}
public void AddUser( String name, String password)
{
try
{
PreparedStatement stm=con.prepareStatement("insert into user value(?,?)");
stm.setString(1,name);
stm.setString(2,password);
try
{
stm.executeQuery();
}catch(Exception e)
{
}
con.close();
}
catch(Exception e)
{
}
}