java导入文本文件代码 java怎么导入文本( 三 )


sbf.append("insert into users values('"+uuid+"',"+ null+","+Constants.MALE+","+null+","+null+",'60.176.36.250','"+formatDateTime()+"',"+null+","+null+","+null+","+null+","+null+","+null+",0,"+null+","+null+",0,0,0,'"+formatDateTime()+"','1036',0,"+null+","+null+","+null+","+null+","+null+",'11',"+null+","+null+","+null+","+null+","+null+");/n");
sbf.append("insert into user_user_group values('"+uuid+"','"+ uuid +"','"+Constants.PERSONAL_USER+"');/n");
UUID = uuid;
str[0]=sbf.toString();
str[1]=UUID;
return str;
}
public String formatDateTime(){
Date date = new Date();
/**
* 时间格式化2009-12-31 09:04:31
*/
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
return sdf.format(date);
}
private String tranStr(String oldstr) {
String newstr = "";
try {
newstr = new String(oldstr.getBytes("ISO-8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return newstr;
}
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
public static boolean isTwoCharacter(String str){
String regEx="[a-zA-Z0-9]{2,3}";
Pattern p=Pattern.compile(regEx);
Matcher m=p.matcher(str);
return m.find();
}
}
2、连接数据库执行数据导入
import java.sql.DriverManager;
import java.sql.ResultSet;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
/**
* 插入数据库 生成sql语句
*
* created on Mar 8, 2010
* @see
*/
public class InsertDB {
private static final String user = "pintu";
private static final String pwd = "pintu";
private static final String url = "jdbc:mysql://192.168.10.6:3306/pintu";
private static final String driver = "com.mysql.jdbc.Driver";
public static Connection getCon() {
Connection con = null;
try {
Class.forName(driver).newInstance();
con = (Connection) DriverManager.getConnection(url, user, pwd);
if (con != null) {
System.out.println("你已连接到数据库:" + con.getCatalog());
}
} catch (Exception e) {
System.out.println("连接数据库失败!");
e.printStackTrace();
}
return con;
}
public boolean insertDB(String FltNum) {
Connection con = null;
Statement stm = null;
boolean flag = false;
String sql = "insert into t_FltPsgInfo values('" + FltNum +
//"','"
//+ FltLine + "','" + FltDate + "','" + PsgName + "','" + PsgType
//
//+ "','" + PsgSex + "','" + PsgCab + "','" + PsgSeatNo + "','"
//
//+ PsgInfo +
"')";
try {
con = getCon();
stm = (Statement) con.createStatement();
int i = stm.executeUpdate(sql);
if (i0) {
flag = true;
System.out.println(flag + "插入数据成功!");
}
} catch (Exception e) {
flag = false;
e.printStackTrace();
} finally {
close(null, stm, con);
}
return flag;
}
//关闭相关连接
public void close(ResultSet rs, Statement stm, Connection con) {
if (rs != null)
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
if (stm != null)
try {
stm.close();
} catch (Exception e) {
e.printStackTrace();
}
if (con != null)
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
java代码 如何向TXT文件写入内容?向txt文件写入内容基本思路就是获得一个file对象java导入文本文件代码,新建一个txt文件java导入文本文件代码,打开I/O操作流,使用写入方法进行读写内容,示例如下java导入文本文件代码:

推荐阅读