虚心求教:我想用EXCEL或者access做一个数据库,并能实现我们内网上的 *** 作和自动生成一些文件,

虚心求教:我想用EXCEL或者access做一个数据库,并能实现我们内网上的 *** 作和自动生成一些文件,,第1张

可用jdbc方式连接EXCEL。实现增、删、改、查。

例:查询

package gzxtest;

import javasql;

public class dbexcel {

/

  连接池对象

 /

private static String ExcelDataSource = "driver={Microsoft Excel Driver (xls)};DBQ=c:/testxls";

/

  返回Excel数据连接的实例

 

  @throws javalangException

  @return Connection

 /

public synchronized static Connection GetConnectionInstance() throws Exception {

Connection lcon = null;

try {

String url = "jdbc:odbc:" + ExcelDataSource;

ClassforName("sunjdbcodbcJdbcOdbcDriver");

lcon = DriverManagergetConnection(url, "", "");

} catch (Exception e) {

Systemoutprintln("EXCEL数据源连接失败。"+egetMessage());

}

return lcon;

}

/

   将Excel 数据批量入库

 

 /

public static boolean ExcelVolumeToDB() throws Exception

{

//读取Excel 数据

String esql = "select 姓名,性别  from [Sheet1$A1:B5]";

Systemoutprintln("查询数据: " + esql);

Connection ExcelCon = GetConnectionInstance();

try

{

Statement ExcelStmt= ExcelConcreateStatement();

ResultSet ExcelRes = ExcelStmtexecuteQuery(esql);

while(ExcelResnext()){

Systemoutprintln("姓名: " + ExcelResgetString("姓名") + "、性别: " + ExcelResgetString("性别"));

}

ExcelResclose();

ExcelStmtclose();

ExcelConclose();

return true;

} catch (Exception ex) {

exprintStackTrace();

return false;

}

}

public static void main(String[] args) throws Exception {

ExcelVolumeToDB();

}

}

首先spring整合mybatis在(一)中说过,那么下面说一下如何自动生成dao、pojo、mapperxml文件

第一步、在pomxml中增加一个插件,意义在于手动执行maven的 *** 作:

<plugin>

<groupId>orgmybatisgenerator</groupId>

<artifactId>mybatis-generator-maven-plugin</artifactId>

<version>132</version>

<configuration>

<!-- 自动生成文件配置文件的地址 -->

<configurationFile>src/main/resources/mybatis-generator/generatorConfigxml</configurationFile>

<verbose>true</verbose>

<overwrite>true</overwrite>

</configuration>

<executions>

<execution>

<id>Generate MyBatis Artifacts</id>

<goals>

<goal>generate</goal>

</goals>

</execution>

</executions>

<dependencies>

<dependency>

<groupId>orgmybatisgenerator</groupId>

<artifactId>mybatis-generator-core</artifactId>

<version>132</version>

</dependency>

</dependencies>

</plugin>

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

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

第二步,将自动生成dao及mapper文件的配置文件放在上述配置的目录下:

<xml version="10" encoding="UTF-8">

<!--数据库连接驱动类,URL,用户名、密码 -->

<jdbcConnection driverClass="commysqljdbcDriver"

connectionURL="jdbc:mysql://ipXXX:3306/databaseXXXcharacterEncoding=utf-8"

userId="root" password="rootXXX">

</jdbcConnection>

<javaTypeResolver>

<property name="forceBigDecimals" value="false"/>

</javaTypeResolver>

<!-- 生成(实体)模型的包名和位置-->

<javaModelGenerator targetPackage="comzhypojo" targetProject="src/main/java">

<property name="enableSubPackages" value="true"/>

<property name="trimStrings" value="true"/>

</javaModelGenerator>

<!-- 生成XML映射文件的包名和位置-->

<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">

<property name="enableSubPackages" value="true"/>

</sqlMapGenerator>

<!-- 生成DAO接口的包名和位置-->

<javaClientGenerator type="XMLMAPPER" targetPackage="comzhydao" targetProject="src/main/java">

<property name="enableSubPackages" value="true"/>

</javaClientGenerator>

<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->

<table tableName="t_user" domainObjectName="UserInfo" enableCountByExample="false"

enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"

selectByExampleQueryId="false"></table>

</context>

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

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

这里有个小点需要注意,在maven工程基于“约定大于配置”的特点,java文件在src/main/java下,所以你需要将targetProject的值设为src/main/java,直接放在你的工程目录下,这样生成后不需要修改文件的引入地址。–~

第三步,就可以maven菜单里手动执行mybatis-generator生成dao层文件了。

以上就是关于虚心求教:我想用EXCEL或者access做一个数据库,并能实现我们内网上的 *** 作和自动生成一些文件,全部的内容,包括:虚心求教:我想用EXCEL或者access做一个数据库,并能实现我们内网上的 *** 作和自动生成一些文件,、springxml文件怎么生成数据库dao、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9280029.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存