mysql创建表格、导入数据、导出到eclipse

1 打开mysql,创建数据库

Create database gongzibiao

2 调用数据库,并且创建表salary1

Use gongzibiao

create table salary1(id int ,name varchar(50),department varchar(50),salary int);

3 导入外部txt数据

LOAD DATA LOCAL INFILE 'e://qing.txt' INTO TABLE salary1;

4 查看表

Select*from salary1;

5 导出到eclipse,下载jar包。

6 在eclipse中新建java项目,命名为sqldemo,将jar包导入到该项目中。在该项目中建立java源文件,命名为emp.java,源文件的代码如下:

java 复制代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class emp {

    public static void main(String[] args) {
        //声明Connection对象
        Connection con;
        //驱动程序名
        String driver = "com.mysql.jdbc.Driver";
        //URL指向要访问的数据库名mydata
        String url = "jdbc:mysql://localhost:3306/gongzibiao";
        //MySQL配置时的用户名
        String user = "root";
        //MySQL配置时的密码
        String password = "1234567890";
        //遍历查询结果集
        try {
            //加载驱动程序
            Class.forName(driver);
            //1.getConnection()方法,连接MySQL数据库!!
            con = DriverManager.getConnection(url,user,password);
            if(!con.isClosed())
                System.out.println("Succeeded connecting to the Database!");
            //2.创建statement类对象,用来执行SQL语句!!
            Statement statement = con.createStatement();
            //要执行的SQL语句
            String sql = "select * from salary1";
            //3.ResultSet类,用来存放获取的结果集!!
            ResultSet rs = statement.executeQuery(sql);
            System.out.println("-----------------------------");
            System.out.println("执行结果如下所示:");  
            System.out.println("-----------------------------");  
            System.out.println("工号"+ "\t"+"姓名" + "\t" + "部门"+"\t"+"薪资");  
            System.out.println("-----------------------------");  
             
            String department = null;
            String name = null;
            String id = null;
            String salary = null;
            while(rs.next()){
               
            	department = rs.getString("department");
                name = rs.getString("name");
                id = rs.getString("id");
                salary = rs.getString("salary");

                //输出结果
                System.out.println(id+"\t"+name + "\t"+department+ "\t" + salary );
            }
            rs.close();
            con.close();
        } catch(ClassNotFoundException e) {   
            //数据库驱动类异常处理
            System.out.println("Sorry,can`t find the Driver!");   
            e.printStackTrace();   
            } catch(SQLException e) {
            //数据库连接失败异常处理
            e.printStackTrace();  
            }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            System.out.println("数据库数据成功获取!!");
        }
    }

}	

4.数据导入eclipse的结果如下图所示

相关推荐
ClouGence1 小时前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
Java水解3 小时前
Mysql查看执行计划、explain关键字详解(超详细)
后端·mysql
知其然亦知其所以然7 小时前
MySQL 社招必考题:如何优化查询过程中的数据访问?
后端·mysql·面试
DemonAvenger8 小时前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
程序新视界9 小时前
如何在MySQL中创建聚集索引?
mysql
AAA修煤气灶刘哥20 小时前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
程序新视界21 小时前
学习MySQL绕不开的两个基础概念:聚集索引与非聚集索引
mysql
RestCloud1 天前
跨境数据传输:ETL如何处理时区与日期格式差异
mysql·api
RestCloud1 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术1 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql