Home > Database > Mysql Tutorial > 如何使用JSP访问MySQL数据库_MySQL

如何使用JSP访问MySQL数据库_MySQL

WBOY
Release: 2016-06-01 14:09:02
Original
927 people have browsed it

  import ="java.util.*"
import ="java.io.*"
contentType="text/html; charset=gb2312"
%>


使用MYsql数据库




使用JSP读取Mysql数据库


String host="localhost"; //数据库主机
String database="pinghui"; //数据库名
String user="test"; //用户名
String pass=""; //口令
%>java.sql.Statement sqlStmt; //语句对象
java.sql.ResultSet sqlRst; //结果集对象
//登记JDBC驱动对象
Class.forName ("org.gjt.mm.mysql.Driver").newInstance ();
//连接数据库
sqlConn= java.sql.DriverManager.getConnection ("jdbc:mysql://"+host+"/"+database,user,pass);
//创建语句对象
sqlStmt=sqlConn.createStatement (java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
//执行Sql语句
String sqlQuery="select count(*) from comment";
sqlRst=sqlStmt.executeQuery (sqlQuery);
sqlRst.next();
int count=sqlRst.getInt(1); //取得总的留言记录数
sqlQuery="select * from comment order by userid desc;";
sqlRst=sqlStmt.executeQuery (sqlQuery); //执行SQL语句
%>
总共有条留言


  
  
  
  
  
  
  
  
  
  
  



  
  
  
  
  
  
  
  
  
  
  


 ID 昵称 性别 地址 IP地址 邮编 Oicq ICQ 电话 留言 时间


//关闭结果集对象
  sqlRst.close();
  //关闭语句对象
sqlStmt.close ();
//关闭数据库连接
sqlConn.close();
%>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template