用java连接数据库啊。下面给点例子可以参考,不大好,凑合看
import java.sql.Connectionimport java.sql.DriverManager
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
public class JdbcConnection
{
private String driver
private String dbname
private String username
private String password
private String url
private Connection con
private PreparedStatement pstmt
private ResultSet rs
public JdbcConnection(){
this.driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"
this.dbname=数据库名"
this.username="数据库账号"
this.password="数据库密码"
this.url="地址加数据库名"
}
//创建连接方法
public Connection getconnection()
{
try
{
Class.forName(driver)
con=DriverManager.getConnection(url,username,password)
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace()
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace()
}
return con
}
//关闭连接方法
public void coloseConnection(Connection con,ResultSet rs,PreparedStatement pstmt)
{
if(rs!=null)
{
try
{
rs.close()
} catch (SQLException e)
{
e.printStackTrace()
}
}
if(pstmt!=null)
{
try
{
pstmt.close()
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace()
}
}
if(con!=null)
{
try
{
con.close()
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
public Connection getCon()
{
return con
}
public void setCon(Connection con)
{
this.con = con
}
public PreparedStatement getPstmt()
{
return pstmt
}
public void setPstmt(PreparedStatement pstmt)
{
this.pstmt = pstmt
}
public ResultSet getRs()
{
return rs
}
public void setRs(ResultSet rs)
{
this.rs = rs
}
public String getDriver()
{
return driver
}
public void setDriver(String driver)
{
this.driver = driver
}
public String getDbname()
{
return dbname
}
public void setDbname(String dbname)
{
this.dbname = dbname
}
public String getUsername()
{
return username
}
public void setUsername(String username)
{
this.username = username
}
public String getPassword()
{
return password
}
public void setPassword(String password)
{
this.password = password
}
public String getUrl()
{
return url
}
public void setUrl(String url)
{
this.url = url
}
}
参考下面方法:import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.IOException
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.apache.poi.hssf.usermodel.HSSFCell
import org.apache.poi.hssf.usermodel.HSSFRow
import org.apache.poi.hssf.usermodel.HSSFSheet
import org.apache.poi.hssf.usermodel.HSSFWorkbook
public class TestExcel {
//记录类的输出信息
static Log log = LogFactory.getLog(TestExcel.class)
//获取Excel文档的路径
public static String filePath = "D://excel.xls"
public static void main(String[] args) {
try {
// 创建对Excel工作簿文件的引用
HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(filePath))
// 在Excel文档中,第一张工作表的缺省索引是0
// 其语句为:HSSFSheet sheet = workbook.getSheetAt(0)
HSSFSheet sheet = wookbook.getSheet("Sheet1")
//获取到Excel文件中的所有行数
int rows = sheet.getPhysicalNumberOfRows()
//遍历行
for (int i = 0i <rowsi++) {
// 读取左上端单元格
HSSFRow row = sheet.getRow(i)
// 行不为空
if (row != null) {
//获取到Excel文件中的所有的列
int cells = row.getPhysicalNumberOfCells()
String value = ""
//遍历列
for (int j = 0j <cellsj++) {
//获取到列的值
HSSFCell cell = row.getCell(j)
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
break
case HSSFCell.CELL_TYPE_NUMERIC:
value += cell.getNumericCellValue() + ","
break
case HSSFCell.CELL_TYPE_STRING:
value += cell.getStringCellValue() + ","
break
default:
value += "0"
break
}
}
}
// 将数据插入到mysql数据库中
String[] val = value.split(",")
TestEntity entity = new TestEntity()
entity.setNum1(val[0])
entity.setNum2(val[1])
entity.setNum3(val[2])
entity.setNum4(val[3])
entity.setNum5(val[4])
entity.setNum6(val[5])
TestMethod method = new TestMethod()
method.Add(entity)
}
}
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)