1. MySQL的安装:
安装链接 : http://www.mysql.com
选安装到这里选择第二个服务端主机:
设置Root密码 :
找到下载的MySQL地址 , 复制地址:
打开环境变量的配置 , 找到PATH , 编辑 , 配置新建环境变量 , 粘贴地址 , 三次确定 :
例如TOMCAT的安装(详情看上文)
配置完成,登录MySQL:
输入刚才配置的Root密码登录,出现下面界面配置成功:
2. Navicat的安装与配置 :
安装链接 : http://www.navicat.com.cn
安装完成后双击打开创建连接MySQL :
新建数据库test01 :
3. SQL基本操作 :
查询数据 :
select * from tableName;
select * from tableName where condition;
插入数据 :
和表创建的顺序一致 :
insert into tableName values(value1 , value2,…);
可以颠倒顺序 :
insert into tableName(field1 ,field2 , …) values(value1 ,value2 ,…)
修改数据 :
update tableName set field1=newValue1, field2=newValue2 where condition
删除数据 :
①不会删除表的结构:
delete from tableName where condition
②会删除表的结构:
drop
eg :
表- - - >查询- - ->新建查询
Ⅰ.创建id , 并让其自动生长
Ⅱ.创建用户名为字符串型
Ⅲ.创建密码为字符串型
Ⅳ.创建primary key来确认是否匹配id
然后创建新表进行增删改查的练习 :
增加 :
insert into member(username,pwd) values("tom","123456");
删除 :
delete from member where id='3';
修改(更新) :
update member set username='zzb' where id='4';
查询 :
select * from member;JDBC基本操作
1. JDBC连接数据库基本步骤:
①.加载驱动程序 :
创建一个java项目 , 名为JDBCTest
添加Java包
(java包我放在资源里了,需要的小伙伴可以自取)
在src中新建类 :
提前须知 :
①驱动程序 :
com.mysql.jdbc.Driver
②连接字符串URL :
"jdbc:mysql://localhost:3306/databaseName"
eclipse中代码如下 :
package com.lucene.dbutil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBCoonection {
public static void main(String[] args)
{
String url = "jdbc:mysql://localhost:3306/test01";
String user = "root";
String password = "123456";
try {
//步骤1:加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//步骤2:建立数据库连接对象
try {
Connection conn = DriverManager.getConnection(url , user ,password);
if(conn != null)
{
System.out.println("数据库连接成功");
}
else {
System.out.println("数据库连接失败");
}
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch (ClassNotFoundException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果如下 , 证明数据库连接成功 :
2. 在eclipse中的增删改查 :
查询 :
package com.lucene.dbutil;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBCoonection {
public static void main(String[] args)
{
String url = "jdbc:mysql://localhost:3306/test01";
String user = "root";
String password = "123456";
try {
//步骤1:加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//步骤2:建立数据库连接对象
try {
Connection conn = DriverManager.getConnection(url , user ,password);
//步骤3:创建执行sql语句的Statement对象实例
java.sql.Statement stmt = conn.createStatement();
//步骤4:使用Statement对象实例向数据库发送SQL语句
ResultSet rs = stmt.executeQuery("select * from member");
while(rs.next())
{
System.out.println("id:"+rs.getInt("id"));
System.out.println(" username"+rs.getString(2));
System.out.println(" pwd"+rs.getString("pwd"));
}
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch (ClassNotFoundException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果 :
增加 :
①
int affectedRows = stmt.executeUpdate("insert into member(username,pwd) values('wusidisi','112233')");
if(affectedRows>0)
{
System.out.println("数据库添加成功");
}
else {
System.out.println("数据库添加失败");
}
②
int affectedRows = stmt.executeUpdate("insert into member(username,pwd) value('tom','112233')");
if(affectedRows>0)
{
System.out.print("数据库添加成功");
}
else
{
System.out.print("数据库添加失败");
}
运行结果 :
在wusidisi后 , 添加了3次tom :
修改 :
int affectedRows = stmt.executeUpdate("update member set pwd='000000' where id=9");
if(affectedRows>0)
{
System.out.print("数据库更新成功");
}
else
{
System.out.print("数据库更新失败");
}
运行结果 :
删除 :
int affectedRows = stmt.executeUpdate("delete from member where id=8");
if(affectedRows>0)
{
System.out.print("数据库删除成功");
}
else
{
System.out.print("数据库删除失败");
}
运行结果 :



