jsp mysql
add MySQL JDBC Driver - mysql-connector-java-5.1.6-bin.jar to tomcat WEB-INF/lib direcotry
MySQL
================
sudo /etc/init.d/mysqld start
mysql -u root -p
create database mysql_test
use mysql_test
create table users(id int not null primary key auto_increment, name varchar(30), pwd varchar(40));
insert into users(id, name, pwd) value(1, "aa", "11");
insert into users(id, name, pwd) value(2, "bb", "22");
================
JSP page
<%@ page contentType="text/html; charset=gb2312" language="java"
import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<%
//**************************************
//********* JDBC_ODBC连接MySql数据库,不需要设置数据源
//*********************************/
//********** 数据库连接代码 开始 ******/
//以下几项请自行修改
String server="localhost"; //MYSQL 服务器的地址
String dbname="mysql_test"; //MYSQL 数据库的名字
String user="root"; //MYSQL 数据库的登录用户名
String pass="xxxxxx"; //MYSQL 数据库的登录密码
String port="3306"; //SQL Server 服务器的端口号,默认为1433
//数据库连接字符串
String url ="jdbc:mysql://"+server+":"+port+"/"+dbname+"?user="+user+"&password="+pass+"&useUnicode=true&characterEncoding=GB2312";
//加载驱动程序
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//建立连接
Connection conn= DriverManager.getConnection(url);
//创建语句对象
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
// **** 数据库连接代码 结束 *******
String sql="select * from users";
ResultSet rs=stmt.executeQuery(sql);
//rs.first();
while(rs.next()){
out.print("用户名:");
out.print(rs.getString("name")+" 密码:");
out.println(rs.getString("pwd")+"<br>");
}
rs.close();
stmt.close();
conn.close();
%>