mysql数据库操作

完整教程:how2j

备忘

  • 连接数据库 导jar包 和代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    public class TestJDBC {
    public static void main(String[] args) {

    //初始化驱动
    try {
    //驱动类com.mysql.jdbc.Driver
    //就在 mysql-connector-java-5.0.8-bin.jar中
    //如果忘记了第一个步骤的导包,就会抛出ClassNotFoundException
    Class.forName("com.mysql.jdbc.Driver");

    // 建立与数据库的Connection连接
    // 这里需要提供:
    // 数据库所处于的ip:127.0.0.1 (本机)
    // 数据库的端口号: 3306 (mysql专用端口号)
    // 数据库名称 how2java
    // 编码方式 UTF-8
    // 账号 root
    // 密码 admin
    Connection c = DriverManager
    .getConnection(
    "jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8",
    "root", "admin");

    System.out.println("连接成功,获取连接对象: " + c);

    System.out.println("数据库驱动加载成功 !");

    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    }
  • 对数据库增删查改 Statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
...
Statement s = c.createStatement();

// 准备sql语句
// 注意: 字符串要用单引号'
String sql = "insert into hero values(null,"+"'提莫'"+","+313.0f+","+50+")";
s.execute(sql);

System.out.println("执行插入语句成功");
```

- 关闭连接:数据库的连接是有限资源,相关操作结束后,养成关闭数据库的好习惯
先关闭Statement
后关闭Connection
```java
Connection c = null;
Statement s = null;
try {
Class.forName("com.mysql.jdbc.Driver");

c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8", "root",
"admin");

s = c.createStatement();

String sql = "insert into hero values(null," + "'提莫'" + "," + 313.0f + "," + 50 + ")";

s.execute(sql);

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 数据库的连接时有限资源,相关操作结束后,养成关闭数据库的好习惯
// 先关闭Statement
if (s != null)
try {
s.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 后关闭Connection
if (c != null)
try {
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

idea 连接mysql数据库

  • 和eclipse一样 注意如何导包