bitsCN.com
1.前提是MyEclipse已经能正常开发Java工程
2.安装MySQL
个人使用的是版本是 mysql-5.0.22-win32.zip
网址:http://www.mysql.com/downloads/mysql/#downloads
3.下载JDBC驱动
个人使用的是 mysql-connector-java-5.1.22.zip,所需要的就是解压缩之后其中的 mysql-connector-java-5.1.22-bin.jar
网址:http://www.mysql.com/downloads/connector/j/
4.代码测试
package ts.jsj.lyh;
import java.sql.*;
/** *//**
* 使用JDBC连接数据库MySQL的过程
* DataBase:JSJ, table:student;
* @author DuChangfeng 2008 09 18
*/
public class JDBCTest {
public static Connection getConnection() throws SQLException,
java.lang.ClassNotFoundException
{
//第一步:加载MySQL的JDBC的驱动
Class.forName("com.mysql.jdbc.Driver");
//取得连接的url,能访问MySQL数据库的用户名,密码;jsj:数据库名
String url = "jdbc:mysql://localhost:3306/jsj";
String username = "root";
String password = "111";
//第二步:创建与MySQL数据库的连接类的实例
Connection con = DriverManager.getConnection(url, username, password);
return con;
}
public static void main(String args[]) {
try
{
//第三步:获取连接类实例con,用con创建Statement对象类实例 sql_statement
Connection con = getConnection();
Statement sql_statement = con.createStatement();
/** *//************ 对数据库进行相关操作 ************/
//如果同名数据库存在,删除
//sql_statement.executeUpdate("drop table if exists student");
//执行了一个sql语句生成了一个名为student的表
//sql_statement.executeUpdate("create table student (id int not null auto_increment, name varchar(20) not null default 'name', math int not null default 60, primary key (id) ); ");
//向表中插入数据
//sql_statement.executeUpdate("insert student values(1, 'liying', 98)");
//sql_statement.executeUpdate("insert student values(2, 'jiangshan', 88)");
//sql_statement.executeUpdate("insert student values(3, 'wangjiawu', 78)");
//sql_statement.executeUpdate("insert student values(4, 'duchangfeng', 100)");
//---以上操作不实用,但是列出来作为参考---
//第四步:执行查询,用ResultSet类的对象,返回查询的结果
String query = "select * from student";
ResultSet result = sql_statement.executeQuery(query);
/** *//************ 对数据库进行相关操作 ************/
System.out.println("Student表中的数据如下:");
System.out.println("------------------------");
System.out.println("学号" + " " + "姓名" + " " + "数据成绩 ");
System.out.println("------------------------");
//对获得的查询结果进行处理,对Result类的对象进行操作
while (result.next())
{
int number = result.getInt("sno");
String name = result.getString("sname");
String mathScore = result.getString("sgrade");
//取得数据库中的数据
System.out.println(" " + number + " " + name + " " + mathScore);
}
//关闭连接和声明
sql_statement.close();
con.close();
} catch(java.lang.ClassNotFoundException e) {
//加载JDBC错误,所要用的驱动没有找到
System.err.print("ClassNotFoundException");
//其他错误
System.err.println(e.getMessage());
} catch (SQLException ex) {
//显示数据库连接错误或查询错误
System.err.println("SQLException: " + ex.getMessage());
}
}
}
以上大部分内容整理自网络,感谢猿猿们的无私奉献~~具体的步骤、强大的互联网上都比较容易查询的到,这里不再赘述,现加上几点个人认为需要注意的地方:
1)关于mysql-connector-java-5.1.22-bin.jar 的存放位置。在MyEclipse具体的java工程中新建一存放jar 包的文件夹(如 lib),将mysql-connector-java-5.1.22-bin.jar 复制到文件夹中,选中jar包右击--->Build Path--->Add To Build Path,即可。
若出现
ClassNotFoundExceptioncom.mysql.jdbc.Driver
的提示,则正是由于缺少导入jar包所造成的。
2)如果已经对MySQL的使用很熟悉,则可忽略这条。个人在测试连接时,老是出现这样的异常提示:
SQLException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
这正是由于个人对MySQL使用不熟悉,对MySQL进行了诸多尝试性的操作,不知何时无意中将MySQL的服务(如果在安装MySQL时没有更改的话,缺省服务名就是MySQL)关闭,解决方法开启此服务即可。控制面板--->管理工具--->服务--->MySQL--->选择启用。
3)在使用上面的代码测试时,需要更改的地方有:
//MySQL数据库的用户名,密码,数据库名
String url = "jdbc:mysql://localhost:3306/jsj";
String username = "root";
String password = "111";
以及具体基本表中的所要查询的字段名:
int number = result.getInt("sno");
String name = result.getString("sname");
String mathScore = result.getString("sgrade");
多多分享,有问题欢迎交流~~bitsCN.com

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于索引优化器工作原理的相关内容,其中包括了MySQL Server的组成,MySQL优化器选择索引额原理以及SQL成本分析,最后通过 select 查询总结整个查询过程,下面一起来看一下,希望对大家有帮助。

sybase是基于客户/服务器体系结构的数据库,是一个开放的、高性能的、可编程的数据库,可使用事件驱动的触发器、多线索化等来提高性能。

visual foxpro数据库文件是管理数据库对象的系统文件。在VFP中,用户数据是存放在“.DBF”表文件中;VFP的数据库文件(“.DBC”)中不存放用户数据,它只起将属于某一数据库的 数据库表与视图、连接、存储过程等关联起来的作用。

数据库系统由4个部分构成:1、数据库,是指长期存储在计算机内的,有组织,可共享的数据的集合;2、硬件,是指构成计算机系统的各种物理设备,包括存储所需的外部设备;3、软件,包括操作系统、数据库管理系统及应用程序;4、人员,包括系统分析员和数据库设计人员、应用程序员(负责编写使用数据库的应用程序)、最终用户(利用接口或查询语言访问数据库)、数据库管理员(负责数据库的总体信息控制)。

microsoft sql server是Microsoft公司推出的关系型数据库管理系统,是一个全面的数据库平台,使用集成的商业智能(BI)工具提供了企业级的数据管理,具有使用方便可伸缩性好与相关软件集成程度高等优点。SQL Server数据库引擎为关系型数据和结构化数据提供了更安全可靠的存储功能,使用户可以构建和管理用于业务的高可用和高性能的数据应用程序。

go语言可以写数据库。Go语言和其他语言不同的地方是,Go官方没有提供数据库驱动,而是编写了开发数据库驱动的标准接口,开发者可以根据定义的接口来开发相应的数据库驱动;这样做的好处在于,只要是按照标准接口开发的代码,以后迁移数据库时,不需要做任何修改,极大方便了后期的架构调整。

mysql查询为什么会慢,关于这个问题,在实际开发经常会遇到,而面试中,也是个高频题。遇到这种问题,我们一般也会想到是因为索引。那除开索引之外,还有哪些因素会导致数据库查询变慢呢?

结构层次是“数据库→数据表→记录→字段”;字段构成记录,记录构成数据表,数据表构成了数据库。数据库是一个完整的数据的记录的整体,一个数据库包含0到N个表,一个表包含0到N个字段,记录是表中的行。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.