zoukankan
html css js c++ java
JSP连接MySQL数据库的方法
<%
@ page contentType
=
"
text/html; charset=gb2312
"
%>
<%
@ page language
=
"
java
"
%>
<%
@ page
import
=
"
com.mysql.jdbc.Driver
"
%>
<%
@ page
import
=
"
java.sql.*
"
%>
<%
//
驱动程序名
String driverName
=
"
com.mysql.jdbc.Driver
"
;
//
数据库用户名
String userName
=
"
root
"
;
//
密码
String userPasswd
=
"
123
"
;
//
数据库名
String dbName
=
"
stock
"
;
//
表名
String tableName
=
"
users
"
;
//
联结字符串
String url
=
"
jdbc:mysql://localhost/
"
+
dbName
+
"
?user=
"
+
userName
+
"
&password=
"
+
userPasswd;
Class.forName(
"
com.mysql.jdbc.Driver
"
).newInstance();
Connection conn
=
DriverManager.getConnection(url);
Statement stmt
=
conn.createStatement();
String sql
=
"
SELECT * FROM
"
+
tableName;
ResultSet rs
=
stmt.executeQuery(sql);
//
获得数据结果集合
ResultSetMetaData rsmd
=
rs.getMetaData();
//
确定数据集的列数,亦字段数
int
numColumns
=
rsmd.getColumnCount();
//
输出每一个列名
out.print(
"
<table border=1><tr>
"
);
for
(
int
i
=
1
;i
<
numColumns;i
++
)
{
out.print(
"
<td>
"
+
rsmd.getColumnName(i)
+
"
</td>
"
);
}
out.print(
"
</tr>
"
);
while
(rs.next())
{
out.print(
"
<tr>
"
);
for
(
int
i
=
1
;i
<
numColumns;i
++
)
{
//
输出数据
out.print(
"
<td>
"
+
rs.getString(i)
+
"
</td>
"
);
}
out.print(
"
</tr>
"
);
}
out.print(
"
</table>
"
);
out.print(
"
数据库操作成功,恭喜你
"
);
rs.close();
stmt.close();
conn.close();
%>
MySql的jdbc下载地址:
http://mysql.isu.edu.tw/Downloads/Connector-J/mysql-connector-java-5.1.6.zip
查看全文
相关阅读:
pgsql 记录
tomcat下放两个spring boot项目
nigex 反向代理
tomcat7里放springboot
postgresql 建表语句
从最大似然到EM算法浅解(转载)
深度学习资料
【vuejs小项目——vuejs2.0版本】组件化的开发方式
【vuejs小项目——vuejs2.0版本】单页面搭建
如何关闭eslint
原文地址:https://www.cnblogs.com/ringwang/p/1236816.html
最新文章
VB使用SSTAB控件需要注意的问题
SQL 语句一些注意的地方
VB利用api删除元素
VB 导出数据到Excel
VB6利用正则表达式验证IP地址合法性
打开VB开发工具提示:Imagelist来自mscomctl.ocx控件出错,可能是mscomctl.ocx过期,解决方法
转载-VB中禁用文本框右键菜单等功能
表单
iframe内联框架
媒体元素
热门文章
策略模式
HTML标签
HTML基本概念
单例模式
线程池
线程通信
同步锁Lock
linux
java httpclient跳过https证书验证
Windows修改管理员用户名密码
Copyright © 2011-2022 走看看