Idea的jdbc菜鸟的自我修养+踩坑经历

离谱了,换了电脑之后装好了mysql,导入了jar包,add了;

但是却找不到db1的表,
报错内容:Unknown database 'db1'   他不认识我的数据表了!!!查了很多教程依旧没有解决(后来发现自己是猪猪 明明就是连数据库 找什么表= =)

很多教程用db2,其实是人家在mysql里创建了新的库叫db2,下面是std表等等,而不是说在mysql这个库下面创建表db2哦!!!


 

 

 

 

package cn.it.com;

import java.sql.*;
import java.sql.DriverManager;
import java.sql.Connection;

public class test {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "root");

            String sql = "select *from db1";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            System.out.println(rs);

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}

然后我用cmd代码 发现进入mysql之后确实也找不到我的表,但是又是可以成功登录mysql的!

 

 很好!我就去找了到底为啥我就是本地找不到表

所以我用win+r 重启了mysql,重新cmd之后得到了这个

mysql> show tables;
ERROR 1046 (3D000): No database selected
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)

mysql> use mysql
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| db1                       |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| student                   |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
26 rows in set (0.00 sec)

mysql> select*from db1;
+----+------+------+------+
| id | age  | name | time |
+----+------+------+------+
|  1 |   12 | 123  | NULL |
|  2 |   12 | 123  | NULL |
|  3 |   12 | 123  | NULL |
|  4 |   12 | 213  | NULL |
|  5 |   12 | 123  | NULL |
+----+------+------+------+
5 rows in set (0.00 sec)

mysql>
View Code

 果然是连的上的- - 最后第二天起床发现 这个压根就不是连表啊 就是连mysql就可以了- - 我就是猪猪 不说了悲伤了

但是随之来的问题:

Error:(15, 46) java: 不兼容的类型: int无法转换为java.sql.ResultSet

这时 我们就要复习我们的笔记- - executeQuery是用来查询的 而增上改 是要executeUpdate方法

 

 

package cn.it.com;

import java.sql.*;
import java.sql.DriverManager;
import java.sql.Connection;

public class test {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "root");

            String sql = "INSERT INTO db1(age,NAME,TIME) VALUE(28,\"tt\",NULL)";
            Statement stmt = conn.createStatement();
            int i = stmt.executeUpdate(sql);
            System.out.println(i);

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}

这是修改之后的代码~ 返回结果1~~

结束啦!!!!

有什么问题欢迎提问,也欢迎一起讨论交流!!!

posted @ 2022-01-24 08:55  Tityaaaa  阅读(115)  评论(0编辑  收藏  举报