CSDN的高手们帮帮我!
现在我从一个接口中查询到这些数据(在WEB页面里面看到),我怎样用JAVA程序把这些数据插入数据库? 中间是用TAB分开的。一个字段对应一个,我已经建好了表。请大家帮帮小弟
13817626572 0 1 2009-2-5 14:40:00 2009-2-5 14:49:00 电脑 上海市 联通
13061682563 0 1 2009-2-5 14:40:00 2009-2-5 14:40:00 电脑 上海市 联通
......
现在我从一个接口中查询到这些数据(在WEB页面里面看到),我怎样用JAVA程序把这些数据插入数据库? 中间是用TAB分开的。一个字段对应一个,我已经建好了表。请大家帮帮小弟
13817626572 0 1 2009-2-5 14:40:00 2009-2-5 14:49:00 电脑 上海市 联通
13061682563 0 1 2009-2-5 14:40:00 2009-2-5 14:40:00 电脑 上海市 联通
......
然后你 可以用 substring, indexOf(),split()..这些函数来处理
完全可以把字符串split,
然后会得到10个字符串,
然后将其中4个字符串,也就是日期和时间 合并一下,插入到Out_time和Show_Time。
其他的顺次插入就可以了。
不大就直接用JDBC插就可以了,可以把每一行做为一个pojo对象,下面是一个构造POJO对象并且插入数据库的代码.
String str = "13817626572 0 1 2009-2-5 14:40:00 2009-2-5 14:49:00 电脑 上海市 联通";
String str1[]=str.split(" ");
System.out.println(str1[0]);
System.out.println(str1[1]);
System.out.println(str1[2]);
System.out.println(str1[3]+" "+str1[4]);
System.out.println(str1[5]+" "+str1[6]);
System.out.println(str1[7]);
System.out.println(str1[8]);
System.out.println(str1[9]);
}
public class LocalData {
private String phoneNum;
private String Send_State;
private String View_Status;
private String out_time;
private String show_time;
private String type;
private String city;
private String source; public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getSend_State() {
return Send_State;
}
public void setSend_State(String send_State) {
Send_State = send_State;
}
public String getView_Status() {
return View_Status;
}
public void setView_Status(String view_Status) {
View_Status = view_Status;
}
public String getOut_time() {
return out_time;
}
public void setOut_time(String out_time) {
this.out_time = out_time;
}
public String getShow_time() {
return show_time;
}
public void setShow_time(String show_time) {
this.show_time = show_time;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;public class Insert2Db {
//解析每一行数�? 返回�?个Localdata对象
public LocalData getData(String data) {
LocalData object = new LocalData();
String[] dataForm = data.split(" ");
object.setPhoneNum(dataForm[0]);
object.setSend_State(dataForm[1]);
object.setView_Status(dataForm[2]);
object.setOut_time(dataForm[3] + " " + dataForm[4]);
object.setShow_time(dataForm[5] + " " + dataForm[6]);
object.setType(dataForm[7]);
object.setCity(dataForm[8]);
object.setSource(dataForm[9]);
return object;
}
//得到insert语句
public String getSql(LocalData object){
StringBuffer sql = new StringBuffer();
sql.append("insert into demo values ('" + object.getPhoneNum() +"',");
sql.append("'" + object.getSend_State() +"',");
sql.append("'" + object.getView_Status() +"',");
sql.append("toDate('" + object.getOut_time() +"'),");
sql.append("toDate('" + object.getShow_time() +"'),");
sql.append("'" + object.getType() +"',");
sql.append("'" + object.getCity() +"',");
sql.append("'" + object.getSource() +"')");
object = null;
return sql.toString();
}
//得到数据库连接 返回Connection对象
public Connection getConnection(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
// 建立连接
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/demo", "test", "test");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//入库
public void execute(Connection conn,String sql){
Statement stmt = null;
try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
conn.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void insert2Db(String data){
LocalData object = getData(data);
String sql = getSql(object);
execute(getConnection(),sql);
}
public static void main(String[] args){
new Insert2Db().insert2Db("13817626572 0 1 2009-2-5 14:40:00 2009-2-5 14:49:00 电脑 上海市 联通 ");
}
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>
Addproduct
</title>
</head>
<body bgcolor="#ffffff">
<h1>
增加一个新的商品
</h1>
<form action="addproductservlet">
产品id:<input type="text" name="id"/><br>
产品名称:<input type="text" name="name" /><br>
产品价格:<input type="text" name="price" /><br/>
产品描述:<input type="text" name="description" /><br/>
<input type="submit" value="提 交" />
</form>
</body>
</html>
-------------------------------------------------
/***
* 添加一条数据
*/
public boolean insertProduct(ProductBean product){
Connection con = null;
PreparedStatement pstmt = null;
int result =0;
String strSql = "insert into product values(?,?,?,?)";
try{
con = ConnectionDB.createConnection();
pstmt = con.prepareStatement(strSql);
pstmt.setString(1,product.getProductId());
pstmt.setString(2,product.getProductName());
pstmt.setFloat(3,product.getProductPrice());
pstmt.setString(4,product.getProductDescription());
result = pstmt.executeUpdate();
}catch(SQLException se){
se.printStackTrace();
}
return result >0 ? true : false;
}
-----------------------------------------------
package com.product.servlet;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.product.beans.ProductBean;
import com.product.daos.ProductDao;public class AddProductServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables
public void init() throws ServletException {
} //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String id = request.getParameter("id");
String name = request.getParameter("name");
float price =Float.parseFloat( request.getParameter("price"));
String description = request.getParameter("description");
ProductBean productBean = new ProductBean();
productBean.setProductId(id);
productBean.setProductName(name);
productBean.setProductPrice(price);
productBean.setProductDescription(description);
ProductDao productDao = new ProductDao();
if(productDao.insertProduct(productBean)){
out.println("<h1>增加商品成功</h1>");
out.println("<a href =index.jsp>返回</a>");
} out.close();
} //Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
} //Clean up resources
public void destroy() {
}
}
----------------
看.........
import java.io.*;public class MyRegex {
public static void main(String[] args) {
String str="13817626572 0 1 2009-2-5 14:40:00 2009-2-5 14:49:00 电脑 上海市 联通 ";
str=str.trim();//去掉首尾空格
str=str.replaceAll(" {2,}", " ");//把多余空格转化成1个空格
String[] temp=str.split(" ");//根据空格把字符串隔开放在数组里面
//把数组里面的数据一一对应起来
String Phone=temp[0];
String Send_State=temp[1];
String View_Status=temp[2];
String Out_time=temp[3];
String Show_Time=temp[4];
String City=temp[5];
String Source=temp[6];
//SQL insert语句你自己来写吧
}
}