我用的是myeclipse8.5和mysql,驱动已装好,jsp界面已经做好 ,但怎么传值到数据库和怎么把数据显示到tabl

2024-12-30 04:02:14
推荐回答(4个)
回答1:

用的是什么框架? 会用hibernate框架就很简单了
没用框架那就用SQL语句连接数据库
代码如下
String url="jdbc:mysql://localhost:3306/pubs";
String username="sa";
String password="123456";
String driver="com.mysql.jdbc.Driver";
Connection cn;
Statement st=null;
//声明结果集
ResultSet rs=null;
try {
//加载驱动
Class.forName(driver);
//连接数据库
cn=DriverManager.getConnection(url, username, password);
st=cn.createStatement();
//执行SQL语句
st.executeUpdate("insert into users values('li','123546')");
rs= st.executeQuery("select * from stu");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
//注:我没有声明事务

回答2:

很囧,难道c#和java很大区别,方法是差不多的,楼上正解,当然还有其他的方法

回答3:

图显IP:
发我吧,帮你弄弄23023023

回答4:

package DB;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConnection {
static Connection con =null;
static Statement statement = null;
public static void getConnection() throws Exception {
//1.加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//指定数据库的url
String url="jdbc:mysql://localhost/wangye";
// 连接数据库的用户名
String UserName ="root";
//连接数据库的密码
String Password ="";

// 2.通过数据库驱动获取数据库连接
con=DriverManager.getConnection(url,UserName,Password);
if(con!=null){
System.out.println("连接数据库成功!");
//3.创建Statement 对象
statement = con.createStatement();

}else{
System.out.println("连接数据库失败?");
}
}
public static ResultSet query(String sql) throws SQLException{
statement = con.createStatement();
// 查询 公用方法 (执行sql语句)
ResultSet result = statement.executeQuery(sql);
return result;
}

// 更新
/* Update 方法 返回的类型为int 表示更新了几条
*
*/
public static int Update(String sql) throws Exception{
statement = con.createStatement();
int rs =statement.executeUpdate(sql);
return rs;
}
//4.关闭数据库
public void CloseDBConnection(){
if(statement !=null){
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

----------------------------------第一段----------------------------------------
package DB;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Users {

// 查询用户信息
public ResultSet findUserName(String userName) {
ResultSet rs = null;
String sql = "select * from t_users where userName='" + userName + "'";

try {
DBConnection.getConnection();
rs = DBConnection.query(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}

// 获取密码
public ResultSet findUsersInfo() {
ResultSet rs = null;
String sql = "select * from t_users";

try {
DBConnection.getConnection();
rs = DBConnection.query(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}

// 注册页面 插入注册信息 email 不能带下划线 不要返回值
/*
* 在数据库语句中 使用参数时 参数一般不带引号 字符串带有引号,
*/
public void registInfo(String userName, String password, String sex,
String age, String grown, String xingzuo, String email) {
String sql = "insert into t_users( userName, password, sex,age, grown,xingzuo, email)"
+ "values( '"
+ userName
+ "', '"
+ password
+ "', '"
+ sex
+ "', '"
+ age
+ "', '"
+ grown
+ "', '"
+ xingzuo
+ "','"
+ email + "')";

try {
DBConnection.getConnection();

DBConnection.Update(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// 查询注册信息
public ResultSet findUserInfo(String id) {
ResultSet rs = null;
String sql = "select * from t_users where id='" + id + "'";

try {
DBConnection.getConnection();
rs = DBConnection.query(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
return rs;
}

// 插入板块 板块添加
public void inserthostInfo(String hostName, String host, String createtime) {
String sql = "insert into t_host( hostName, host, createtime)"
+ "values( '" + hostName + "', '" + host + "', '" + createtime
+ "')";

try {
DBConnection.getConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
DBConnection.Update(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// 查询板块
public ResultSet findhostInfo() {
ResultSet rs = null;
String sql = "select * from t_host ";

try {
DBConnection.getConnection();
rs = DBConnection.query(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}

// 查询板块中信息
public ResultSet findtieziInfo(String hostId) {
ResultSet rs = null;
String sql = "select * from t_content where hostId='" + hostId + "' ";

try {
DBConnection.getConnection();
rs = DBConnection.query(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
// 插入帖子
public ResultSet Inserttiezi(String hostId,String title, String writer, String Content,
String createtime) throws Exception {
ResultSet rs = null;
String sql = "insert into t_content(hostId,title,writer,Content,createtime)values('"
+ hostId
+ "','"
+ title
+ "','"
+ writer
+ "','"
+ Content
+ "','"
+ createtime
+ "')";

try {
DBConnection.getConnection();
DBConnection.Update(sql);
} catch (Exception e) {
}
return rs;
}

// 查询帖子

// 插入回复帖子
public ResultSet Insertbacktiezi(String back, String backwriter,
String createtime, String time) throws Exception {
ResultSet rs = null;
String sql = "insert into t_back(back,backwriter,createtime)values('"
+ back + "','" + backwriter + "','" + createtime + "')";
try {
DBConnection.getConnection();
DBConnection.Update(sql);

} catch (Exception e) {
// TODO: handle exception
}
return rs;
}
// 查询回复帖子
public ResultSet findbacktieziInfo(String backId) {
ResultSet rs = null;
String sql = "select * from t_back where backId='" + backId + "' ";

try {
DBConnection.getConnection();
rs = DBConnection.query(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;

-----------------------------第二段----------------------------------------------------------

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DB.Users;

public class login extends HttpServlet {

/**
* Constructor of the object.
*/
public login() {
super();
}

/**
* Destruction of the servlet.

*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet.

*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("");
out.println("");
out.println(" A Servlet");
out.println(" ");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" ");
out.println("");
out.flush();
out.close();
}

/**
* The doPost method of the servlet.

*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

//创建数据库访问对象 从页面获取的用户名
Users u= new Users();
// 从页面获取的用户名 密码
String userName =request.getParameter("userName");
String password=request.getParameter("password");
String btnvalue =request.getParameter("btnvalue");

//如果按钮为loginbtn 则实行登录逻辑
if("loginbtn".equals("btnvalue")){

// 查询用户信息
String Password ="";
//密码
ResultSet rs = u.findUserName(userName);
//如果数据库中有值,则说明用户存在 则从数据库中取出密码 与从页面取出的密码判断
if(rs!=null){
try{
while(rs.next()){
password=rs.getString("password");
}
}catch(Exception e){

}
}
if(password.equals(Password)){
ResultSet rs1 = u.findUserName(userName);
try {
String id = rs1.getString("id");
System.out.println("登陆Action"+id);
// 获取session对象
HttpSession session = request.getSession();
session.setAttribute("userNum", id);
response.sendRedirect("success.jsp?id="+id);
} catch (SQLException e) {

e.printStackTrace();
}

}
}else{

response.sendRedirect("Login.jsp");
}

response.sendRedirect("regist.jsp");

//判断用户名密码是否正确
}

private void SendDiredict(String string) {
// TODO Auto-generated method stub

}

/**
* Initialization of the servlet.

*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
----------------------第三段-----------------------------------------

<%@ page language="java" import="java.util.*,java.sql.ResultSet" pageEncoding="gbk"%>
<%@ page import="DB.*"%>

<%
//导入包 时 别忘记包名需要用引号 导入的时候,别忘了import
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>






My JSP 'regsucc.jsp' starting page











返回主页
<%String id = request.getParameter("id"); %>
<% Users u=new Users();

ResultSet rs=u.findUserInfo("1");
%>












<%if(rs!=null){
while(rs.next()){
%>











用户名

密码

性别

年龄

生肖

星座

邮箱

<%=rs.getString("userName") %>

<%=rs.getString("password") %>

<%=rs.getString("sex") %>

<%=rs.getString("age") %>

<%=rs.getString("grown") %>

<%=rs.getString("xingzuo") %>

<%=rs.getString("email") %>

<% }

} else{

System.out.println("该用户信息无法查看");
}%>




-----------------------第四段---------------------------------------------------

补充 这个是我做的网站的Jsp和servlet 代码。
传值到数据库中 需要编写插入方法 像Insertbacktiezi()方法,
第一段代码是连接数据库的步骤。 为公用方法。
第二段为自己编写的 增 删 改 查的方法
根据需求,你需要的是插入insert 和select 语句吧
第三段为 登录的servlet处理 主要是select 语句
第四段是页面查询显示 因为这个是我们做的第一个设计,我想比hibernate 简单,因为我还没学那个呢。希望对你有帮助。
在象数据库插值时 要注意插入的列数 ,是否匹配。还要注意 传参数的类型。在查值时容易发生空指针异常,需要用用调错模式。