javatxtmysql的簡單介紹

本文目錄一覽:

用java如何將txt文件導入mysql

Java中將txt文件導入到mysql基本的思路就是先使用I/O操作流獲取到文件具體信息,然後將信息拼接成mysql插入到資料庫中,示例如下:

1、先讀取txt文件的內容,文件內容可以按照一定的規律進行排列,這樣程序讀取就方便。

import java.io.IOException;

import java.io.RandomAccessFile;

import java.io.UnsupportedEncodingException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import com.config.Constants;

import com.utils.UUIDUtil;

/**

* txt文本數據 採集類

*

* @see

*/

public class UserDataGather {

public static final String TXT_FILE_PATH = “D://testUser.txt”;

public static final String openFileStyle = “r”;

public static final String fieldLimitChar = “.”;

public static final int fieldAllCount = 1;

public static final String default_password = “PTMD0309”;

public Integer count = 0;

private String FltNum;

public String UUID;

/**

* 功能:解析文本文件

*/

public void loadFile() {

try {

RandomAccessFile raf = new RandomAccessFile(TXT_FILE_PATH, openFileStyle);

String line_record = raf.readLine();

while (line_record != null) {

// 解析每一條記錄

parseRecord(line_record);

line_record = raf.readLine();

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 功能:具體解析每一條記錄,這裡可以增加很多對記錄的解析判斷條件,如是否為字母、

*/

@SuppressWarnings(“static-access”)

private void parseRecord(String line_record) throws Exception {

//拆分記錄

// String[] fields = line_record.split(fieldLimitChar);

// System.out.println(tranStr(line_record)+”Ok”);

String temp = line_record.substring(line_record.indexOf(fieldLimitChar, 0), line_record.indexOf(” “, line_record.indexOf(fieldLimitChar, 0)));

// if (fields.length == fieldAllCount) {

//

FltNum = tranStr(temp).trim().replace(fieldLimitChar,””).replace(” “,””);

// System.out.println(FltNum);

if(FltNum.length()=4){

if(!isNumeric(FltNum)){

// System.out.println(generateSql(FltNum)[0].toString());

// System.out.println(generateSql(FltNum)[1].toString());

count++;

String[] temp1 = generateSql(FltNum);

MyFile mf = new MyFile();

mf.creatTxtFile(“insertPinTuUserSql”);

mf.writeTxtFile(temp1[0].toString());

mf.creatTxtFile(“UUID”);

mf.writeTxtFile(temp1[1].toString()+”,”);

}

}else if(FltNum.length() ==2 || FltNum.length() ==3){

if(!isNumeric(FltNum)){

if(!isTwoCharacter(FltNum)){

// System.out.println(generateSql(FltNum)[0].toString());

// System.out.println(generateSql(FltNum)[1].toString());

count++;

String[] temp2 = generateSql(FltNum);

MyFile mf = new MyFile();

mf.creatTxtFile(“insertPinTuUserSql”);

mf.writeTxtFile(temp2[0].toString());

mf.creatTxtFile(“UUID”);

mf.writeTxtFile(temp2[1].toString()+”,”);

}

}

}

// InsertDB db = new InsertDB();

//

// db.insertDB(FltNum);

// }

}

@SuppressWarnings(“static-access”)

public String[] generateSql(String userName) throws IOException{

StringBuffer sbf = new StringBuffer();

String[] str = new String[2];

String uuid = UUIDUtil.getUUID();

sbf.append(“insert into user values(‘”+uuid+”‘,'” + userName +”‘,'”+default_password+”‘,”+Constants.ENABLED+”,”+Constants.NUllDELETE+”,”+Constants.AUDITING+”,'”+uuid+”@164.com’,'”+formatDateTime()+”‘,”+Constants.REGEDIT_USER+”);/n”);

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 (i 0) {

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文件導入mysql?

Java中將txt文件導入到mysql基本的思路,操作流獲取到文件具體信息,然後將信息拼接成mysql插入到資料庫中。

1.先讀取txt文件的內容,文件內容可以按照一定的規律進行排列,這樣程序讀取就方便。

2.java.io.IOExceptionimportjava.io.RandomAccessFileimportjava.io.UnsupportedEncodingException。

3.import java.text.SimpleDateFormat,import java.util.Date, import java.util.regex.Matcher,

import java.util.regex.Pattern

4.import com.config.Constants; import com.utils.UUIDUtil;

5.public static final String TXT_FILE_PATH =「D://testUser.txt。

public static final String openFileStyle =「r」。

如何使用Java將把讀取的txt文件內容寫進MySQL

String str=”將txt文件內容寫到一個字元串中”;

然後用

insert str into table

這樣的insert語句插入到資料庫中,當然前提條件是資料庫中要存在這樣的一個資料庫表。

對txt文件進行分割,逐個讀進資料庫,可能比較耗時!

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/232424.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-11 12:51
下一篇 2024-12-11 12:51

相關推薦

  • Python簡單數學計算

    本文將從多個方面介紹Python的簡單數學計算,包括基礎運算符、函數、庫以及實際應用場景。 一、基礎運算符 Python提供了基礎的算術運算符,包括加(+)、減(-)、乘(*)、除…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • Python海龜代碼簡單畫圖

    本文將介紹如何使用Python的海龜庫進行簡單畫圖,並提供相關示例代碼。 一、基礎用法 使用Python的海龜庫,我們可以控制一個小海龜在窗口中移動,並利用它的「畫筆」在窗口中繪製…

    編程 2025-04-29
  • Python櫻花樹代碼簡單

    本文將對Python櫻花樹代碼進行詳細的闡述和講解,幫助讀者更好地理解該代碼的實現方法。 一、簡介 櫻花樹是一種圖形效果,它的實現方法比較簡單。Python中可以通過turtle這…

    編程 2025-04-28
  • Python大神作品:讓編程變得更加簡單

    Python作為一種高級的解釋性編程語言,一直被廣泛地運用於各個領域,從Web開發、遊戲開發到人工智慧,Python都扮演著重要的角色。Python的代碼簡潔明了,易於閱讀和維護,…

    編程 2025-04-28
  • 用Python實現簡單爬蟲程序

    在當今時代,互聯網上的信息量是爆炸式增長的,其中很多信息可以被利用。對於數據分析、數據挖掘或者其他一些需要大量數據的任務,我們可以使用爬蟲技術從各個網站獲取需要的信息。而Pytho…

    編程 2025-04-28
  • 如何製作一個簡單的換裝遊戲

    本文將從以下幾個方面,為大家介紹如何製作一個簡單的換裝遊戲: 1. 遊戲需求和界面設計 2. 使用HTML、CSS和JavaScript開發遊戲 3. 實現遊戲的基本功能:拖拽交互…

    編程 2025-04-27
  • Guava Limiter——限流器的簡單易用

    本文將從多個維度對Guava Limiter進行詳細闡述,介紹其定義、使用方法、工作原理和案例應用等方面,並給出完整的代碼示例,希望能夠幫助讀者更好地了解和使用該庫。 一、定義 G…

    編程 2025-04-27
  • 2的32次方-1:一個看似簡單卻又複雜的數字

    對於計算機領域的人來說,2的32次方-1(也就是十進位下的4294967295)這個數字並不陌生。它經常被用來表示IPv4地址或者無符號32位整數的最大值。但實際上,這個數字卻包含…

    編程 2025-04-27
  • 製作一個簡單的管理系統的成本及實現

    想要製作一個簡單的管理系統,需要進行技術選型、開發、測試等過程,那麼這個過程會花費多少錢呢?我們將從多個方面來闡述製作一個簡單的管理系統的成本及實現。 一、技術選型 當我們開始思考…

    編程 2025-04-27

發表回復

登錄後才能評論