用的是什么框架? 会用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();
}
//注:我没有声明事务
很囧,难道c#和java很大区别,方法是差不多的,楼上正解,当然还有其他的方法
图显IP:
发我吧,帮你弄弄23023023
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("
用户名 | 密码 | 性别 | 年龄 | 生肖 | 星座 | 邮箱 |
<%=rs.getString("userName") %> | <%=rs.getString("password") %> | <%=rs.getString("sex") %> | <%=rs.getString("age") %> | <%=rs.getString("grown") %> | <%=rs.getString("xingzuo") %> | <%=rs.getString("email") %> |