jsp系统怎么连接数据库

jsp系统怎么连接数据库,第1张

请问下是什么结构?用jdbc连接的话

public class DBUtil {

private static String user;

private static String password;

private static String url;

static{

Properties prop=new Properties();

try {

ClassLoader classLoader=DBUtilclassgetClassLoader();

InputStream is=classLoadergetResourceAsStream("dbproperties");

propload(is);

user=propgetProperty("user");

password=propgetProperty("password");

url=propgetProperty("url");

ClassforName("commysqljdbcDriver");

} catch (Exception e) {

eprintStackTrace();

throw new RuntimeException("找不到加载类");

}

}

public static Connection getConnection()throws Exception{

Connection conn=null;

conn=DriverManagergetConnection(url,user,password);

return conn;

}

public static void close(Connection conn){

if(conn!=null){

try {

connclose();

} catch (SQLException e) {

eprintStackTrace();

}

}

}

public static void main(String[] args)throws Exception {

Systemoutprintln(DBUtilgetConnection());

}

}

如果是用SSH架构的话,用hibernate里面去配置就OK了!

难得讲:

直接连接和关闭代码:

private static final String DRIVER = "commicrosoftsqlserverjdbcSQLServerDriver";// 驱动类

private static final String URL = "jdbc:sqlserver://localhost:1433;DataBaseName=paipaiDB";// 连接URL地址

private static final String USER = "sa";// 数据库用户名

private static final String PWD = "123";// 数据库用户密码

/

与数据库建立连接

@return

@throws ClassNotFoundException

@throws SQLException

/

public static Connection getCon() throws ClassNotFoundException,

SQLException {

Connection con = null;

ClassforName(DRIVER);

con = DriverManagergetConnection(URL, USER, PWD);

return con;

}

/

关闭所有与数据库的连接对象

@param res

结果集对象

@param pstat预编义对象

@param con连接对象

/

public static void closeAll(ResultSet res, PreparedStatement pstat,

Connection con) {

if (res != null) {

try {

resclose();

} catch (SQLException e) {

// TODO 自动生成 catch 块

eprintStackTrace();

}

}

if (pstat != null) {

try {

pstatclose();

} catch (SQLException e) {

// TODO 自动生成 catch 块

eprintStackTrace();

}

}

try {

if (con != null && !conisClosed()) {

conclose();

}

} catch (SQLException e) {

// TODO 自动生成 catch 块

eprintStackTrace();

}

}

1通过jdbc连接上数据库,并从中获取一个连接。(建议由一个工具类提供)

2创建一个jsp页面、一个servlet类和一个service业务逻辑类。

3当点击查询按钮时调用servlet并把文本框中的参数传递过去。

4在servlet中获取页面传递过来的参数,并调用service中方法(此方法负责条件查询并返回list集合)

5servlet中把查询集合放到request作用域并转发到jsp页面进行迭代,把数据取出展示即可。

以上就是关于jsp系统怎么连接数据库全部的内容,包括:jsp系统怎么连接数据库、JSP怎样链接数据库、如何用一张JSP页面连接数据库,实现查询,修改 *** 作等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9370716.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存