跟着我做就是了,这个可是我现场测试和编写的哦!!没有任何copy
(1)把mysql的驱动放到tomcat的lib中 驱动是这个
>
请问下是什么结构?用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了!
JSP使用数据库的例子:
showByJdbcOdbcjsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="javasql" %>
<HTML><BODY bgcolor=cyan>
<% Connection con;
Statement sql;
ResultSet rs;
try{ ClassforName("sunjdbcodbcJdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{ outprint(e);
}
try { con=DriverManagergetConnection("jdbc:odbc:mymoon","sa","sa");
sql=concreateStatement();
rs=sqlexecuteQuery("SELECT FROM employee WHERE salary>3000");
outprint("<table border=2>");
outprint("<tr>");
outprint("<th width=100>"+"雇员号");
outprint("<th width=100>"+"姓名");
outprint("<th width=50>"+"出生日期");
outprint("<th width=50>"+"薪水");
outprint("</TR>");
while(rsnext())
{ outprint("<tr>");
outprint("<td >"+rsgetString(1)+"</td>");
outprint("<td >"+rsgetString(2)+"</td>");
outprint("<td >"+rsgetDate("birthday")+"</td>");
outprint("<td >"+rsgetFloat("salary")+"</td>");
outprint("</tr>") ;
}
outprint("</table>");
conclose();
}
catch(SQLException e)
{ outprint(e);
}
%>
</BODY></HTML>
在数据库提取部分数据,在JSP上显示的做法如下:
思路:1、创建db连接 2、创建statement 3、执行查询 4、遍历结果并展示
完整代码如下:
<span style="font-size:12px;"><span style="font-size:14px;"><%@ page language="java" import="javasql,javaio,javautil"%>
<%@ page contentType="text/html;charset=utf-8"%>
<html>
<head>
<style type="text/css">
table {
border: 2px #CCCCCC solid;
width: 360px;
}
td,th {
height: 30px;
border: #CCCCCC 1px solid;
}
</style>
</head>
<body>
<%
//驱动程序名
String driverName = "commysqljdbcDriver";
//数据库用户名
String userName = "root";
//密码
String userPasswd = "szy";
//数据库名
String dbName = "studentmanage";
//表名
String tableName = "student";
//联结字符串
String url = "jdbc:mysql://localhost:3306/" + dbName + "user="
+ userName + "&password=" + userPasswd;
ClassforName("commysqljdbcDriver")newInstance();
Connection connection = DriverManagergetConnection(url);
Statement statement = connectioncreateStatement();
String sql = "SELECT FROM " + tableName;
ResultSet rs = statementexecuteQuery(sql);
%>
<br>
<br>
<table align="center">
<tr>
<th>
<%
outprint("学号");
%>
</th>
<th>
<%
outprint("姓名");
%>
</th>
<th>
<%
outprint("专业");
%>
</th>
<th>
<%
outprint("班级");
%>
</th>
</tr>
<%
while (rsnext()) {
%>
<tr>
<td>
<%
outprint(rsgetString(1));
%>
</td>
<td>
<%
outprint(rsgetString(2));
%>
</td>
<td>
<%
outprint(rsgetString(3));
%>
</td>
<td>
<%
outprint(rsgetString(4));
%>
</td>
</tr>
<%
}
%>
</table>
<div align="center">
<br> <br> <br>
<%
outprint("数据查询成功,恭喜你");
%>
</div>
<%
rsclose();
statementclose();
connectionclose();
%>
</body>
</html></span><span style="font-size:24px;color: rgb(255, 0, 0);">
</span></span>
\x0d\ \x0d\ \x0d\ \x0d\ \x0d\您的第一个字段内容为: \x0d\您的第二个字段内容为: \x0d\ \x0d\ \x0d\ \x0d\ \x0d\
附加数据库
打开SQL Server 2000中的“企业管理器”,然后展开本地服务器,在“数据库”数据项上单击鼠标右键,在d出的快捷菜单中选择“所有任务”/“附加数据库”菜单项。
将d出“附加数据库”对话框,在该对话框中单击后面那个按钮,选择所要附加数据库的mdf文件,单击“确定”按钮,即可完成数据库的附加 *** 作。
完成之后你就能在你企业管理器中看到你所导入的mdf文件的数据库了
以上就是关于jsp数据库连接全部的内容,包括:jsp数据库连接、jsp页面中实现级联,数据是从数据库中读取的,怎样实现,最好有个例子,以供参考、jsp系统怎么连接数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)