jianglilili / jianglilili/MySQL

实现第一个JDBC程序

Open
#3 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

1. `cd D:\MySQL\mysql-8.0.15-winx64\bin`

2. `D:\MySQL\mysql-8.0.15-winx64\bin>mysqld --initialize --console`

3. `D:\MySQL\mysql-8.0.15-winx64\bin>mysqld install`

4. `D:\MySQL\mysql-8.0.15-winx64\bin>net start mysql`

5. `D:\MySQL\mysql-8.0.15-winx64\bin>mysql -u root -p`
6. `mysql> alter user 'root'@'localhost'indentified by 'youpassword';`

7. ```
```
mysql> use mysql;
Database changed
mysql> CREATE TABLE users(
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(40),
-> password VARCHAR(40),
-> email VARCHAR(40),
-> birthday DATE
-> )CHARACTER SET utf8 COLLATE utf8_general_ci;
```

```
8. ```
mysql> INSERT INTO users(NAME,PASSWORD,email,birthday)
-> VALUES('zs','123456','zs@sina.com','1980-12-04');
Query OK, 1 row affected (0.03 sec)

```
```
mysql> INSERT INTO users(NAME,PASSWORD,email,birthday)
-> VALUES('lisi','123456','lisi@sina.com','1981-12-04');
Query OK, 1 row affected (0.03 sec)
```

```
mysql> INSERT INTO users(NAME,PASSWORD,email,birthday)
-> VALUES('wangwu','123456','wangwu@sina.com','1979-12-04');
Query OK, 1 row affected (0.01 sec)
```

9.```
```
mysql> select*from users;
+----+--------+----------+-----------------+------------+
| id | name | password | email | birthday |
+----+--------+----------+-----------------+------------+
| 1 | zs | 123456 | zs@sina.com | 1980-12-04 |
| 2 | lisi | 123456 | lisi@sina.com | 1981-12-04 |
| 3 | wangwu | 123456 | wangwu@sina.com | 1979-12-04 |
+----+--------+----------+-----------------+------------+
```
```

- 查看端口号
`mysql> show global variables like 'port';`
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set, 1 warning (0.04 sec)

代码实现:
package cn.itcast.jdbc.example;
import java.sql.*;
public class Example01 {
public static void main(String[] args)throws SQLException{
Statement stmt=null;
ResultSet rs=null;
Connection conn=null;
try {
//1.注册数据库的驱动
Class.forName("com.mysql.jdbc.Driver");
//2.通过DriverManager获取数据库连接
String url="jdbc:mysql://localhost:3306/mysql";
String username="root";
String password="youpassword";
conn=DriverManager.getConnection(url, username, password);
//3.通过Connection对象获取Statement对象
stmt=conn.createStatement();
//4.使用Statement执行SQL语句
String sql="select * from users";
rs=stmt.executeQuery(sql);
//5.操作ResultSet结果集
System.out.println("id|name|password|email|birthday");
while(rs.next()) {
int id=rs.getInt("id");
String name=rs.getString("name");
String psw=rs.getString("password");
String email=rs.getString("email");
Date birthday=rs.getDate("birthday");
System.out.println(id+"|"+name+"|"+psw+"|"+email+"|"+birthday);
}
}catch(ClassNotFoundException e)
{
e.printStackTrace();
}finally {
//6.回收数据库资源
if(rs!=null)
{
try {
rs.close();
}catch(SQLException e)
{
e.printStackTrace();
}
rs=null;
}
if(stmt!=null)
{
try {
stmt.close();
}catch(SQLException e)
{
e.printStackTrace();
}
stmt=null;
}
if(conn!=null)
{
try
{
conn.close();
}catch(SQLException e)
{
e.printStackTrace();
}
conn=null;
}
}
}

}

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing the JDBC setup commands and the Example01 program shown in the issue, including its MySQL users table and connection settings. Confirm where this example belongs in the repository; done should mean a documented or runnable first JDBC query that connects to MySQL and prints the users records.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mysql
Domain
database
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.