Home Database Mysql Tutorial JSP连接MySQL数据库攻略_MySQL

JSP连接MySQL数据库攻略_MySQL

Jun 01, 2016 pm 02:11 PM
mysql Order Install Strategy database connect


  一. 软件下载
  
  Mysql
  下载版本:4.1.11
  http://dev.mysql.com/downloads/mysql/4.1.html
  
  JDBC驱动
  下载版本:3.1.8
  http://dev.mysql.com/downloads/connector/j/3.1.html
  
  Mysql界面插件:mysql-front
  下载版本镜像:HongKong(我下回来安装就是中文版了)
  http://www.mysqlfront.de/download.html
  
  二. 软件安装
  
  1.安装mysql
  请参阅相关文章,推荐文章:http://blog.csdn.net/saulzy/archive/2005/04/23/359648.aspx
  
  2.JDBC驱动:mysql-connector-java-3.1.8
  这只是一个压缩包,并不需要安装,只要将其解压,我么使用的是文件夹mysql-connector-java-3.1.8里的文件:mysql-connector-java-3.1.8-bin.jar.
  
  3. Mysql界面插件:mysql-front
  这是一个安装程序,按照提示安装就可以了.
  
  三. 环境配置
  
  首先,我要说明的是我现在tomcat的安装路径是: D:\Program Files\Java\Tomcat;JDK的安装路径是:D:\Program Files\Java\j2sdk。
  
  在这里,需要配置环境变量的是JDBC驱动.在配置前先要把刚才说到的mysql-connector-java-3.1.8-bin.jar本地硬盘某处(我放的地方:D:\Program Files\Java\mysqlforjdbc),然后根据你放的地方,配置classpath,我的配置是这样的:
  
  [被屏蔽广告]  D:\Program files\Java\j2sdk\lib\tools.jar;
  
  D:\Program Files\Java\j2sdk\lib\mysql-connector-java-3.1.8-bin-g.jar;
  
  D:\Program Files\Java\mysqlforjdbc\mysql-connector-java-3.1.8-bin.jar
  配置这个的目的是让你的java应用程序找到连接mysql的驱动.
  
  配置完环境变量后还有很重要一步就是为JSP连接数据库配置驱动,这个其实很简单,就是把mysql-connector-java-3.1.8-bin.jar拷到某些文件夹里就行了,我在网上看了很多资料问了很多人,各种说法都有,我综合了一下,为了保险,我都全做了,呵呵,反正就是拷一个400K的文件而已,现列出要把mysql-connector-java-3.1.8-bin.jar拷进去的文件夹,如下:
  D:\Program Files\Java\Tomcat\common\lib
  D:\Program Files\Java\Tomcat\shared\lib
  
  四. 数据库的使用
  
  Mysql安装完毕以后,还有一些要注意的地方(推荐文章):http://blog.csdn.net/saulzy/archive/2005/04/23/359811.aspx
  
  就象在文章提到的,mysql安装好后最重要一样就是要看数据库有没有作为系统服务启动了,所以在大家进行数据库操作前,应要看看,在操作系统的开始->运行->输入services.msc,确定你在安装时你设置的关于mysql的那个服务已经启动,这样你在操作数据库时不会报连接不上的错误.
  
  上面提到了一个较方便的mysql界面插件,但是这个界面是我在已经开始使用mysql后才找到的,刚开始我是在dos下用命令行进行操作的.虽然那个界面也可以进行建库啊,设定权限等操作,但是,我觉得懂得在使用命令行也是一个很重要的技能,所以我先从命令行开始说,怎样简单使用mysql.到后面会谈及mysql-front的使用.
  
  现在我想在mysql里建一个数据库shujuku,以及在数据库里建一个表biao.具体的命令如下(假设mysql我是刚安装好的)
  
  1. 进入dos状态(记住命令行的要运行在mysql的安装目录下的bin目录的)
  
  2. 连接mysql
  输入:mysql –h localhost –u root –p
  输入在安装时已设好的密码,就近入了mysql的命令编辑界面了。
  
  3. 使用mysql的基本命令(在mysql命令行编辑每输入完命令后最后一定要有分号,不然会报错)
  显示数据库:show databases;
  使用数据库:use 数据库名;
  
  4.建库
  命令:create database shujuku;
  
  5.为数据库设置权限(用户和密码)
  命令:grant all privileges on shujuku.* to test@localhost identified by “123456”;
  当你执行完这个命令以后,只要你再以用户名:test,密码:123456登录时你就只可以对shujuku这个数据库操作,这样避开使用root,对数据库的安全有很大帮助.
  
  6.建表
  命令:create table biao(id int(8) primary key,name varchar(10));
  
  剩下来的与标准sqsl命令基本上是一样的,具体操作略
  值得一提的是,你在命令行上输入"?",就会有mysql命令的简单帮助,如下:
  
  呵呵,那样,我们还可以知道退出,就是"exit",呵呵!
  
  五. 关于mysql-front的使用
  
  我找了好几个mysql的界面工具,觉得最简洁方便还是mysql-front,可惜的是要收费,不过还好有试用期,呵呵,可以先感受感受一下,最重要一点是mysql-front有简体中文版的,英文不好的我用起来就舒服多了.下面简单说说使用吧.
  
  首先,安装就不用说了,有向导,而且很简单.安装好后第一次运行时会跳出来一个要求添加对话的框,在这里你可以添加例如上面已经设定好的shujuku,过程如下:
  当你在注册的复选框里填上你在上面mysql设定好的用户名和密码后,在选择数据库框里就有shujuku 的数据库了,选上,按确定.进入mysql-fron后,你就会看到下面的界面,这是你就可以进行操作了.
  
  要说明的是,你还可以把root用户也加进去,这要你在mysql-fron的界面上选设置->对话->新建,再按上面进行就可以,出了root你还可以加入更多的用户,方法还是一样的,设置不同的用户,是方便对不同数据库进行管理,呵呵,root是权限最高的,可不要随便让别人使用你的root用户,保正你数据库的安全.
  
  六. JSP连接mysql
  
  现在就是尝试用jsp连接mysql了
  我在eclipse里建了个test_mysql.jsp页面,代码如下:
  
  
  
  
  
  
  
  
  
    
  //驱动程序名
  
  String driverName="com.mysql.jdbc.Driver";
  
  //数据库用户名
  
  String userName="cl41";
  
  //密码
  
  String userPasswd="123456";
  
  //数据库名
  
  String dbName="db";
  
  //表名
  
  String tableName="dbtest";
  
  //联结字符串
  
  String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
  
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  
  Connection connection=DriverManager.getConnection(url);
  
  Statement statement = connection.createStatement();
  
  String sql="SELECT * FROM "+tableName;
  
  ResultSet rs = statement.executeQuery(sql);
  
  //获得数据结果集合
  
  ResultSetMetaData rmeta = rs.getMetaData();
  
  //确定数据集的列数,亦字段数
  
  int numColumns=rmeta.getColumnCount();
  
  // 输出每一个数据值
  
  out.print("id");
  
  out.print("|");
  
  out.print("num");
  
  out.print("
");
  
  while(rs.next()) {
  
  out.print(rs.getString(1)+" ");
  
  out.print("|");
  
  out.print(rs.getString(2));
  
  out.print("
");
  
  }
  
  out.print("
");
  
  out.print("数据库操作成功,恭喜你");
  
  rs.close();
  
  statement.close();
  
  connection.close();
  
  %>
  
  然后把test-_mysql.jsp部署到tomcat处,如何部署可参考"配置Eclpise+tomcat并实现JSP的编写与部署",在浏览器中就可以看到结果了。
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

RDS MySQL integration with Redshift zero ETL RDS MySQL integration with Redshift zero ETL Apr 08, 2025 pm 07:06 PM

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Apr 08, 2025 pm 07:12 PM

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

How to fill in mysql username and password How to fill in mysql username and password Apr 08, 2025 pm 07:09 PM

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

How to optimize MySQL performance for high-load applications? How to optimize MySQL performance for high-load applications? Apr 08, 2025 pm 06:03 PM

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

Understand ACID properties: The pillars of a reliable database Understand ACID properties: The pillars of a reliable database Apr 08, 2025 pm 06:33 PM

Detailed explanation of database ACID attributes ACID attributes are a set of rules to ensure the reliability and consistency of database transactions. They define how database systems handle transactions, and ensure data integrity and accuracy even in case of system crashes, power interruptions, or multiple users concurrent access. ACID Attribute Overview Atomicity: A transaction is regarded as an indivisible unit. Any part fails, the entire transaction is rolled back, and the database does not retain any changes. For example, if a bank transfer is deducted from one account but not increased to another, the entire operation is revoked. begintransaction; updateaccountssetbalance=balance-100wh

See all articles