jsp,数据库操作,这样写对吗?

来源:百度知道 编辑:UC知道 时间:2024/05/27 10:44:29
public class Execute {
static Connection conn=null;
static Statement stmt=null;
static ResultSet rs=null;
Integer updateNo=0;
public static void executeQuery(String sql){
/**
* 查询结果集
*/
try {
conn= DbUtil.getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void executeUpdate(String sql){
/**
* 返回更新
*/
try {
conn= DbUtil.getConnection();
stmt=conn.createStatement();
updateNo=stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void closeConnection(){
/**
* 关闭数据库连接
*/

你可以这样连接数据库
<%@ page language="java" %>
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html; charset=gbk32" %>
<%
Class.forName("com.mysql.jdbc.Driver");
String strConn="jdbc:mysql://localhost/数据库名称"+"?user=数据库名&password=数据库密码"+"&userUnicode=true&characterEncoding=gbk32";
Connection conn= DriverManager.getConnection(strConn);//连接对象
Statement stmt3=conn.createStatement();
String strSql=”sql语句”;
int result=stmt3.executeUpdate(strSql); //运行
%>
以上就是对数据库的操作了。包括连接,对数据库的操作,运行

对的啊
DbUtil 是负责连接数据库的类把。