[英]How to insert data from a text file from Java to SQL server?
I made this source code to try to insert data from a text file but it does not do what I want.我制作了这个源代码来尝试从文本文件中插入数据,但它没有做我想要的。
First I declare variables with assigned values to insert as default values to my table and then just take the new values entered through the text file.首先,我声明具有指定值的变量以作为默认值插入到我的表中,然后仅获取通过文本文件输入的新值。 I do not know the correct insert I should use.我不知道我应该使用正确的插入物。
package Clases;
import java.sql.*;
import java.util.*;
import java.math.*;
public class Importar {
public static Connection Conexión;
public static void main(String[] args) {
try {
Scanner lector = new Scanner(System.in);
String ID_Origin = "NE001";
short siPeriod = 2015;
byte TypeTrans = 0;
String ID_Entry = "";
short siType = 0;
String Concept = "";
String ID_Class = "01";
String ID_Budget = null;
java.sql.Timestamp dtDate = null;
String ID_Cost = null;
java.math.BigDecimal Debits = null;
java.math.BigDecimal Credits = null;
String ID_Currency = null;
BigDecimal AmountFOB = BigDecimal.ZERO;
BigDecimal CurrencyRate = BigDecimal.ZERO;
boolean boCashFlow = false;
short Status = 0;
short StatusConsol = 0;
System.out.println("ID_Entry");
ID_Entry = lector.next();
System.out.println("Concept");
Concept = lector.next();
System.out.println("dtDate");
System.out.println("Debits");
System.out.println("Credits");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Conexión = DriverManager.getConnection(
"jdbc:sqlserver://SSL2541\\SQLEXPRESS:1433;" +
"databaseName=namedatabase;user=sa;password=password");
PreparedStatement enrollItmt;
// This is where I have my confusion and did not return
// anything the program
enrollItmt = Conexión.prepareStatement(
"LOAD FROM 'C://Users//aurbina//Desktop//cargadatabase.txt' "
+ "INSERT INTO tablename( ID_Entry, siType, Concept, ID_Class, "
+ "dtDate, Debits, Credits)"
+ "VALUES (+Concept, +dtDate, +Debits, +Credits)");
enrollItmt.execute();
}
catch(ClassNotFoundException | SQLException ex) {
}
}
}
Here is a simple Sample Example to Insert Text File to SQL Server Table as a help.这是一个简单的示例示例,用于将文本文件插入 SQL Server 表作为帮助。 Can be modified accordingly可以相应修改
import com.microsoft.sqlserver.jdbc.SQLServerBulkCSVFileRecord;
import com.microsoft.sqlserver.jdbc.SQLServerBulkCopy;
import com.microsoft.sqlserver.jdbc.SQLServerBulkCopyOptions;
import java.sql.Types;
import java.util.Date;
import java.sql.*;
import java.util.*;
public class App {
public static void main( String[] args ) {
Connection Conexión;
Conexión = DriverManager.getConnection(
"jdbc:sqlserver://SSL2541\\SQLEXPRESS:1433;" +
"databaseName=namedatabase;user=sa;password=password");
String tablename = "dbo.filetable";
try {
File file = new File("file.txt");
//The Text File has three columns "ID", "FirstName", "Lastname" separated
//by comma as delimiter.
//The Table (tablename) in SQL Server also has same Number of Columns.
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
String line;
while((line=br.readLine())!=null) {
sb.append(line);
sb.append("\n");
}
fr.close();
//try (InputStream inputStream = new FileInputStream(file)) {
try (InputStream inputStream = new
ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8)))
{
//System.out.println("file to be copied to " + tablename);
// Column information
SQLServerBulkCSVFileRecord fileRecord = new
SQLServerBulkCSVFileRecord(inputStream,
StandardCharsets.UTF_8.name(), "\\,", true);
//Here , is delimiter in Test File.
fileRecord.addColumnMetadata(1, "ID", Types.VARCHAR, 0, 0);
fileRecord.addColumnMetadata(2, "FirstName", Types.VARCHAR, 0, 0);
fileRecord.addColumnMetadata(3, "LastName", Types.VARCHAR, 0, 0);
// Setting bulk insert options(batch size). Insert each 10000 rows in
//bulk from N number of rows in Text File.
SQLServerBulkCopyOptions copyOptions = new SQLServerBulkCopyOptions();
copyOptions.setBatchSize(10000);
try (SQLServerBulkCopy bulkCopy = new
SQLServerBulkCopy(Conexión))
{
bulkCopy.setBulkCopyOptions(copyOptions);
bulkCopy.setDestinationTableName(tablename);
bulkCopy.writeToServer(fileRecord);
}
System.out.println("Data from text file Inserted from Java to
SQLserver Done");
} catch (Exception e) {
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.