我想使用CSV阅读器将值上传到oracle数据库,
我有这样的数据库连接
package com.test.examples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @author henry gunawan
*
*/
public class DBConnection
{
public static Connection getConnection() throws SQLException, ClassNotFoundException
{
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@10.40.43.133:1526:opr", "user", "Password");
return connection;
}
public static void main(String[] args)
{
try
{
getConnection();
}
catch (SQLException | ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
我有这样的主班
package com.test.examples;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import com.opencsv.CSVReader;
public class ImportCsv
{
public static void main(String[] args)
{
readCsv();
readCsvUsingLoad();
}
private static void readCsv()
{
try (CSVReader reader = new CSVReader(new FileReader("billy.csv"), ';'); Connection connection = DBConnection.getConnection();)
{
String insertQuery = "Insert into EDC_LOT_PAIRTO_MERCHANT (edc,num, merchant) values (?,?,?)";
PreparedStatement pstmt = connection.prepareStatement(insertQuery);
String[] rowData = null;
int i = 0;
while((rowData = reader.readNext()) != null){
for (String data : rowData)
{
pstmt.setString((i % 3) + 1, data);
if (++i % 3 == 0)
pstmt.addBatch();// add batch
if (i % 30 == 0)// insert when the batch size is 10
pstmt.executeBatch();
}}
System.out.println("Data Successfully Uploaded");
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static void readCsvUsingLoad()
{
try (Connection connection = DBConnection.getConnection())
{
String loadQuery = "LOAD DATA LOCAL INFILE '" + "C:\\billy.csv" + "' INTO TABLE EDC_LOT_PAIRTO_MERCHANT FIELDS TERMINATED BY ';'"
+ " LINES TERMINATED BY '\n' (edc, num, merchant) ";
System.out.println(loadQuery);
Statement stmt = connection.createStatement();
stmt.execute(loadQuery);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
我遇到了这样的错误,好像我的连接是正确的,但是返回给出了错误。
java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:489)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:553)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:254)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:528)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.javacodegeeks.examples.DBConnection.getConnection(DBConnection.java:18)
at com.javacodegeeks.examples.ImportCsv.readCsv(ImportCsv.java:21)
at com.javacodegeeks.examples.ImportCsv.main(ImportCsv.java:14)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:439)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:454)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:693)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:251)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1140)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:340)
... 9 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:149)
at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:405)
... 14 more
在我与mysql的连接中,此代码工作正常,但是当我尝试连接至oracle时,出现此错误。