JSP通过Tomcat连接MySQL的实现(4)

wKiom1kJrQaxfw-dAAA0oGik_FY759.png

 

mysql> create database tomcat;

mysql> use tomcat;

mysql> create table tt(id int,name varchar(128));

mysql> insert into tt values(1,'mk');

mysql> insert into tt values(2,'linuxidc');

ysql> grant all on tomcat.* to tomcat@localhost identified by 'tomcat';

mysql> exit

[root@linuxidc64 ~]# mysql -utomcat -ptomcat #测试tomcat帐号。

12)测试主页:

[root@linuxidc63 ROOT]# pwd

/usr/local/tomcat/webapps/ROOT

#注:复制时,删除多添加%号。如果,直接复制此段内容,最后两行的开头会多出个%,注意把他删除了。[root@linuxidc63 ROOT]# vim test.jsp #写入以下内容:

<%@ page contentType="text/html;charset=utf-8"%>

<%@ page import="java.sql.*"%>

<html>

<body>

<%

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

String url ="jdbc:mysql://localhost/tomcat?user=tomcat&password=tomcat&useUnicode=true&characterEncoding=utf-8";

Connection conn= DriverManager.getConnection(url);

Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

String sql="select * from tt";

ResultSet rs=stmt.executeQuery(sql);

while(rs.next()) {%>

step:<%=rs.getString(1)%>

context:<%=rs.getString(2)%><br><br>

<%}%>

<%out.print("Congratulations!!! JSP connect MYSQL IS OK!!");%>

<%rs.close();

stmt.close();

conn.close();

%>

</body>

</html>

测试

Httpd://192.168.1.63:8080/test.jsp 

wKioL1kJrR_C_5H6AABW6sy94YM187.png

 

更多Tomcat相关教程见以下内容

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/e6401699ec9ed2af3ee3a551bd8ca3f1.html