java多表代码生成,java实现多表查询-成都快上网建站

java多表代码生成,java实现多表查询

怎样用java代码动态生成数据库表

Class.forName("oracle.jdbc.driver.OracleDriver");

作为一家“创意+整合+营销”的成都网站建设机构,我们在业内良好的客户口碑。创新互联公司提供从前期的网站品牌分析策划、网站设计、成都网站建设、网站制作、创意表现、网页制作、系统开发以及后续网站营销运营等一系列服务,帮助企业打造创新的互联网品牌经营模式与有效的网络营销方法,创造更大的价值。

Connection conn=DriverManager.getConnection("数据库url","帐号","密码");

state=conn.createStatement();

state.executeUpdate("create 建表语句");

state.executeUpdate("insert 插入数据")------插入的值由页面获得,注意字符串拼接。

然后就是关闭连接,state.close();conn.close();

核心代码就是这些,具体应用你可以多写几个方法(增删改查),都是类似的,注意异常的处理,关闭连接最好在finally中进行。

如何用java代码将数据库中的数据生成excel表

java 读excel 还是比较方便简单的,原理就是,先用java 读取excel,然后,一行行的写入数据库,字段的话,你自己程序里面写就行了,给你个例子:

从Excel读取数据,生成新的Excel,以及修改Excel

package common.util;

import jxl.*;

import jxl.format.UnderlineStyle;

import jxl.write.*;

import jxl.write.Number;

import jxl.write.Boolean;

import java.io.*;

/**

* Created by IntelliJ IDEA.

* User: xl

* Date: 2005-7-17

* Time: 9:33:22

* To change this template use File | Settings | File Templates.

*/

public class ExcelHandle

{

public ExcelHandle()

{

}

/**

* 读取Excel

*

* @param filePath

*/

public static void readExcel(String filePath)

{

try

{

InputStream is = new FileInputStream(filePath);

Workbook rwb = Workbook.getWorkbook(is);

//Sheet st = rwb.getSheet("0")这里有两种方法获取sheet表,1为名字,而为下标,从0开始

Sheet st = rwb.getSheet("original");

Cell c00 = st.getCell(0,0);

//通用的获取cell值的方式,返回字符串

String strc00 = c00.getContents();

//获得cell具体类型值的方式

if(c00.getType() == CellType.LABEL)

{

LabelCell labelc00 = (LabelCell)c00;

strc00 = labelc00.getString();

}

//输出

System.out.println(strc00);

//关闭

rwb.close();

}

catch(Exception e)

{

e.printStackTrace();

}

}

/**

* 输出Excel

*

* @param os

*/

public static void writeExcel(OutputStream os)

{

try

{

/**

* 只能通过API提供的工厂方法来创建Workbook,而不能使用WritableWorkbook的构造函数,

* 因为类WritableWorkbook的构造函数为protected类型

* method(1)直接从目标文件中读取WritableWorkbook wwb = Workbook.createWorkbook(new File(targetfile));

* method(2)如下实例所示 将WritableWorkbook直接写入到输出流

*/

WritableWorkbook wwb = Workbook.createWorkbook(os);

//创建Excel工作表 指定名称和位置

WritableSheet ws = wwb.createSheet("Test Sheet 1",0);

//**************往工作表中添加数据*****************

//1.添加Label对象

Label label = new Label(0,0,"this is a label test");

ws.addCell(label);

//添加带有字型Formatting对象

WritableFont wf = new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD,true);

WritableCellFormat wcf = new WritableCellFormat(wf);

Label labelcf = new Label(1,0,"this is a label test",wcf);

ws.addCell(labelcf);

//添加带有字体颜色的Formatting对象

WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,

UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);

WritableCellFormat wcfFC = new WritableCellFormat(wfc);

Label labelCF = new Label(1,0,"This is a Label Cell",wcfFC);

ws.addCell(labelCF);

//2.添加Number对象

Number labelN = new Number(0,1,3.1415926);

ws.addCell(labelN);

//添加带有formatting的Number对象

NumberFormat nf = new NumberFormat("#.##");

WritableCellFormat wcfN = new WritableCellFormat(nf);

Number labelNF = new jxl.write.Number(1,1,3.1415926,wcfN);

ws.addCell(labelNF);

//3.添加Boolean对象

Boolean labelB = new jxl.write.Boolean(0,2,false);

ws.addCell(labelB);

//4.添加DateTime对象

jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());

ws.addCell(labelDT);

//添加带有formatting的DateFormat对象

DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");

WritableCellFormat wcfDF = new WritableCellFormat(df);

DateTime labelDTF = new DateTime(1,3,new java.util.Date(),wcfDF);

ws.addCell(labelDTF);

//添加图片对象,jxl只支持png格式图片

File image = new File("f:\\2.png");

WritableImage wimage = new WritableImage(0,1,2,2,image);

ws.addImage(wimage);

//写入工作表

wwb.write();

wwb.close();

}

catch(Exception e)

{

e.printStackTrace();

}

}

/**

* 拷贝后,进行修改,其中file1为被copy对象,file2为修改后创建的对象

* 尽单元格原有的格式化修饰是不能去掉的,我们还是可以将新的单元格修饰加上去,

* 以使单元格的内容以不同的形式表现

* @param file1

* @param file2

*/

public static void modifyExcel(File file1,File file2)

{

try

{

Workbook rwb = Workbook.getWorkbook(file1);

WritableWorkbook wwb = Workbook.createWorkbook(file2,rwb);//copy

WritableSheet ws = wwb.getSheet(0);

WritableCell wc = ws.getWritableCell(0,0);

//判断单元格的类型,做出相应的转换

if(wc.getType == CellType.LABEL)

{

Label label = (Label)wc;

label.setString("The value has been modified");

}

wwb.write();

wwb.close();

rwb.close();

}

catch(Exception e)

{

e.printStackTrace();

}

}

//测试

public static void main(String[] args)

{

try

{

//读Excel

ExcelHandle.readExcel("f:/testRead.xls");

//输出Excel

File fileWrite = new File("f:/testWrite.xls");

fileWrite.createNewFile();

OutputStream os = new FileOutputStream(fileWrite);

ExcelHandle.writeExcel(os);

//修改Excel

ExcelHandle.modifyExcel(new file(""),new File(""));

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

2.在jsp中做相关测试,创建一个writeExcel.jsp

%

response.reset();//清除Buffer

response.setContentType("application/vnd.ms-excel");

File fileWrite = new File("f:/testWrite.xls");

fileWrite.createNewFile();

new FileOutputStream(fileWrite);

ExcelHandle.writeExcel(new FileOutputStream(fileWrite));

%

在IE中浏览writeExcel.jsp就可以动态生成Excel文档了,其中response.setContentType("application/vnd.ms- excel");语句必须要,才能确保不乱码,在jsp中输入%@page contentType="application/vnd.ms- excel;charset=GBK"%不行。

Java动态生成代码

可以的,我说说大概思路,很简单,你自己具体实现吧,把代码写给你没意义的:

1.将你这段字符串输出到一个文件里,用Java类文件的方式命名。

2.调用外部javac命令将该文件编译。

3.用类加载器(ClassLoad)动态加载新的class文件并用Class.forName()注册该类,然后就可以正常使用了。

上面的每一步都能在baidu中找到实现方法,自己发挥吧。

java代码生成器怎么用

zip包,然后自动下载下来

1.预先定义好模板

2.界面输入相关参数

3.解析模板生成代码并下载

最后放出源代码:

package com.et.controller.system.createcode;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import com.et.controller.base.BaseController;

import com.et.util.DelAllFile;

import com.et.util.FileDownload;

import com.et.util.FileZip;

import com.et.util.Freemarker;

import com.et.util.PageData;

import com.et.util.PathUtil;

/**

* 类名称:FreemarkerController

* 创建人:Harries

* 创建时间:2015年1月12日

* @version

*/

@Controller

@RequestMapping(value=”/createCode”)

public class CreateCodeController extends BaseController {

/**

* 生成代码

*/

@RequestMapping(value=”/proCode”)

public void proCode(HttpServletResponse response) throws Exception{

PageData pd = new PageData();

pd = this.getPageData();

/* ============================================================================================= */

String packageName = pd.getString(“packageName”); //包名 ========1

String objectName = pd.getString(“objectName”); //类名 ========2

String tabletop = pd.getString(“tabletop”); //表前缀 ========3

tabletop = null == tabletop?””:tabletop.toUpperCase(); //表前缀转大写

String zindext = pd.getString(“zindex”); //属性总数

int zindex = 0;

if(null != zindext !””.equals(zindext)){

zindex = Integer.parseInt(zindext);

}

ListString[] fieldList = new ArrayListString[](); //属性集合 ========4

for(int i=0; i zindex; i++){

fieldList.add(pd.getString(“field”+i).split(“,fh,”)); //属性放到集合里面

}

MapString,Object root = new HashMapString,Object(); //创建数据模型

root.put(“fieldList”, fieldList);

root.put(“packageName”, packageName); //包名

root.put(“objectName”, objectName); //类名

root.put(“objectNameLower”, objectName.toLowerCase()); //类名(全小写)

root.put(“objectNameUpper”, objectName.toUpperCase()); //类名(全大写)

root.put(“tabletop”, tabletop); //表前缀

root.put(“nowDate”, new Date()); //当前日期

DelAllFile.delFolder(PathUtil.getClasspath()+”admin/ftl”); //生成代码前,先清空之前生成的代码

/* ============================================================================================= */

String filePath = “admin/ftl/code/”; //存放路径

String ftlPath = “createCode”; //ftl路径

/*生成controller*/

Freemarker.printFile(“controllerTemplate.ftl”, root, “controller/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName+”Controller.java”, filePath, ftlPath);

/*生成service*/

Freemarker.printFile(“serviceTemplate.ftl”, root, “service/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName+”Service.java”, filePath, ftlPath);

/*生成mybatis xml*/

Freemarker.printFile(“mapperMysqlTemplate.ftl”, root, “mybatis_mysql/”+packageName+”/”+objectName+”Mapper.xml”, filePath, ftlPath);

Freemarker.printFile(“mapperOracleTemplate.ftl”, root, “mybatis_oracle/”+packageName+”/”+objectName+”Mapper.xml”, filePath, ftlPath);

/*生成SQL脚本*/

Freemarker.printFile(“mysql_SQL_Template.ftl”, root, “mysql数据库脚本/”+tabletop+objectName.toUpperCase()+”.sql”, filePath, ftlPath);

Freemarker.printFile(“oracle_SQL_Template.ftl”, root, “oracle数据库脚本/”+tabletop+objectName.toUpperCase()+”.sql”, filePath, ftlPath);

/*生成jsp页面*/

Freemarker.printFile(“jsp_list_Template.ftl”, root, “jsp/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName.toLowerCase()+”_list.jsp”, filePath, ftlPath);

Freemarker.printFile(“jsp_edit_Template.ftl”, root, “jsp/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName.toLowerCase()+”_edit.jsp”, filePath, ftlPath);

/*生成说明文档*/

Freemarker.printFile(“docTemplate.ftl”, root, “说明.doc”, filePath, ftlPath);

//this.print(“oracle_SQL_Template.ftl”, root); 控制台打印

/*生成的全部代码压缩成zip文件*/

FileZip.zip(PathUtil.getClasspath()+”admin/ftl/code”, PathUtil.getClasspath()+”admin/ftl/code.zip”);

/*下载代码*/

FileDownload.fileDownload(response, PathUtil.getClasspath()+”admin/ftl/code.zip”, “code.zip”);

}

}

如何用JAVA 创建数据库表写出java代码

Connection conn = 链接

Statement stmt = conn.createStatementI();

String sql = "CREATE TABLE PFO_ANALYSE_BRANCH ( "

+" NODE_NAME_S VARCHAR2(50 BYTE), "

+ 其他字段

+")";

stmt.execute(sql)


网页名称:java多表代码生成,java实现多表查询
链接分享:http://kswjz.com/article/dssiodo.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流