1.下载安装mysql,因为开源免费的,直接到官网下载并按提示安装:https://www.mysql.com/downloads/

2.安装完成后查看进程是否有开启mysql(安装mysql一般会自带mysql workbench,这是数据库图形操作界面,我们使用java是不需要用到的)

用cmd(命令行)去操作mysql,在mysql中增添数据库study,然后再study数据库中增加表study

3.下载java的驱动来连接mysql,下载地址:https://dev.mysql.com/downloads/connector/j/,下载得到一个jar包,哪个工程要用到就复制到工程目录下,然后add build加入工程(加入工程后可以在referenced Libraries中找到该jar包)

 

4.java代码:

package com.cn.edu.szu.ming;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/** 
* @ClassName:testdb.java
* @author 2015150308@@email.szu.edu.cn
* @version 创建时间:2017年5月30日 上午11:41:40 
* @Description:简单
*/
public class testdb {
    private final static String URL="jdbc:mysql://localhost:3306/study";       //连接格式jdbc:mysql://(ip地址):(端口)/(数据库名)   
    private final static String NAME="ming";                //mysql用户名
    private final static String PASS="qq147741";            //对应的密码
    static Connection conn;
    static Statement stmt;              //发送静态sql语句(固定键值对)
    static PreparedStatement pst;        //发送动态sql语句,只需改变参数的值(表中对应的值)
    static ResultSet res;                //结果集,用于打印表
    /**
     * 
     * @Tiltle main
     * @param argv
     * @return void
     */
    public static void main(String argv[]){
        try {
            //告诉jvm使用mysql
            Class.forName("com.mysql.jdbc.Driver");         //加载驱动,string为驱动名字
            //连接数据库,得到Connection连接
            conn=DriverManager.getConnection(URL,NAME,PASS);   //DriverManager,    初始化驱动
            System.out.println(conn);
            stmt=conn.createStatement();
            printTable();
            
            
            //修改数据,将第一行的密码改为line1
            pst=conn.prepareStatement("update study set name=?where id=?");
            pst.setString(1, "stu1");     //update的sql语句中第一个参数即pawd,设置为yes
            pst.setInt(2, 1);            //update的sql语句第二个参数设置为
            pst.executeUpdate();       //发送操作
            printTable();
            
            
            
            
            //删除数据,删除表中指定的第一行数据
            pst=conn.prepareStatement("delete from study where id=?");
            pst.setInt(1,3);     //指定上述sql第一个参数即id的值为3
            pst.executeUpdate();
            printTable();
            
            

            //增加行
            pst=conn.prepareStatement("insert into study(id,name,pawd)values(?,?,?)");
            pst.setInt(1,4);                   //设置sql语句中第一个参数(study表中为id)为4
            pst.setString(2,"stu3");
            pst.setString(3,"123456789");
            pst.executeUpdate();
            printTable();
            
            

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        //回收资源
        finally{ 
            try{
                if(conn!=null){
                    conn.close();
                }
                if(stmt!=null){
                    stmt.close();
                }
                if(pst!=null){
                    pst.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        
    }
    
    public static void printTable() throws SQLException{
        //查询表,打印study表
        res=stmt.executeQuery("select * from study");       //选择表中所有信息
        System.out.println("编号"+"\t"+"姓名"+"\t"+"密码");
        while(res.next()){           //打印所有行
            System.out.print(res.getInt(1)+"\t");           //打印study表中每一行的第一个参数值(即id)
            System.out.print(res.getString(2)+"\t");        //打印study表中每一行的第二个参数值(即name)
            System.out.print(res.getString(3)+"\n");        //打印study表中每一行的第三个参数值(即pawd)
            
        }
    }
}

 更详细内容推荐阅读:

(十分推荐)详细描述:http://www.jianshu.com/p/dc73ee0f2f83

sql语法:http://www.w3school.com.cn/sql/sql_syntax.asp

mysql官方文档:https://www.tutorialspoint.com/mysql/index.htm