Excel数据使用jdbc直接插入Mysql数据库
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import jxl.*;
public class ImportExcel {
public static void main(String[] args) {
File importExcel = new File("D:\\test\\test.xls");
try {
// 数据库连接
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/datebaseTest?characterEncoding=UTF-8", "root", "");
PreparedStatement prep = conn
.prepareStatement("insert into importe (id,name) values (?,?)");
Workbook workBook = Workbook.getWorkbook(importExcel);
Sheet[] sheet = workBook.getSheets();
int sheet_i_num = 0;
String id = "";
String name = "";
if (sheet != null && sheet.length > 0) {
for (int sheetNum = 0; sheetNum < sheet.length; sheetNum++) {
sheet_i_num = sheet[sheetNum].getRows();
for (int rowNum = 1; rowNum < sheet_i_num; rowNum++) {
Cell[] cells = sheet[sheetNum].getRow(rowNum);
id = cells[0].getContents();
name = cells[1].getContents();
prep.setInt(1, Integer.parseInt(id));
prep.setString(2, name);
prep.executeUpdate();
System.out.println(id + "--------" + name);
}
}
}
workBook.close();
prep.close();
conn.close();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
}
}
}
在mysql中建立数据表
CREATE TABLE importe (
Id int(11) NOT NULL auto_increment,
name varchar(50) default NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
______________________________________________________________________
向数据库中插入时间,将Excel中时间的格式dd/MM/yyyy插入数据库中,我认为这需要格式转换为yyyy-MM-dd格式。
对于Mysql 建立表格
create table test(
id int(10),
datafrom date);
在Excel中,建立表格,保存为2003的Excel。
Id Data
1 25/11/2009
2 26/11/2009
3 27/11/2009
import java.io.File;
import java.sql.Connec
相关文档:
项目从mysql迁移到ORACLE中遇到移植问题,mysql中支持limit 而ORACLE say no .
解决方法 利用ORACLE的伪列 rownum来控制。。
Mysql : select * from table limit 10
equl
ORACLE: select * from table where rownum <= 10
ORACLE 伪列介绍:
随数据字典一起自动创建的一个表,属于sys模式,任何用户都可以访问,&nbs ......
运行程序可能会报错:Can’t connect to local MySQL Server through socket ‘/tmp/mysql.sock’
这个错误的提示是说,不能通过’/tmp/mysql.sock’连接到服务器。Mysql.sock是创建与mysqld服务器相关的MySQL通信端点所使用的套接字。而PHP标准配置正是通过’/tmp/mysql.sock’来连接 ......
问题:一开始我用Navicat Lite for mysql 将数据库转存储位sql文件。打开之后发现,中文都变成了乱码。
所以 在网上搜索了些资料看看。发觉大家都是用命令导出的。用了之后,中文就不乱码了。解决了我的一个难题。特此记下。
方法:
首先确认 mysql服务是开启的。
然后点“开始”运行输入cmd,进入DOS环境。cd ......
1006:MYSQL 创建数据库失败 ......