用java语言将Access中的数据导入Mysql中,总是出现Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1错误,而且只能导入一条记录,而且导入的中文是乱码,是什么原因呢,请教高手!
import java.sql.*;
import java.util.ArrayList;import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;public class Dao {
private ArrayList list=new ArrayList();
private ArrayList list1=new ArrayList();
private Connection conn;
private Connection conn2;
private Statement st1;
private PreparedStatement st2;
private PreparedStatement st3;
private ResultSet rs;
private String url;
private String url2;
private String user;
private String password;
public Dao() throws SQLException
{
Access();
Mysql();
}
public void Access()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
url="jdbc:odbc:test";
try
{
conn=DriverManager.getConnection(url);
st1=conn.createStatement();
rs=st1.executeQuery("select*from aa");
if(rs.next())
{
list.add(rs.getString("ID"));
list1.add(rs.getString("name"));
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
try{
rs.close();
st1.close();
conn.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
public void Mysql() throws SQLException
{
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
url2="jdbc:mysql://localhost:3306/tt3";
user="root";
password="4691789";
conn2=DriverManager.getConnection(url2,user,password);
try{
st2=conn2.prepareStatement("INSERT INTO course(id,cname_zh)VALUES(?,?)");
for(int i=0;i<=list.size();i++)
{
st2.setInt(1,i);
st2.setString(2, (String)list1.get(i));
st2.addBatch();
st2.executeUpdate();
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
try{
st2.close();
conn.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
public static void main(String args[]) throws SQLException
{
new Dao();
}
}
import java.sql.*;
import java.util.ArrayList;import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;public class Dao {
private ArrayList list=new ArrayList();
private ArrayList list1=new ArrayList();
private Connection conn;
private Connection conn2;
private Statement st1;
private PreparedStatement st2;
private PreparedStatement st3;
private ResultSet rs;
private String url;
private String url2;
private String user;
private String password;
public Dao() throws SQLException
{
Access();
Mysql();
}
public void Access()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
url="jdbc:odbc:test";
try
{
conn=DriverManager.getConnection(url);
st1=conn.createStatement();
rs=st1.executeQuery("select*from aa");
if(rs.next())
{
list.add(rs.getString("ID"));
list1.add(rs.getString("name"));
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
try{
rs.close();
st1.close();
conn.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
public void Mysql() throws SQLException
{
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
url2="jdbc:mysql://localhost:3306/tt3";
user="root";
password="4691789";
conn2=DriverManager.getConnection(url2,user,password);
try{
st2=conn2.prepareStatement("INSERT INTO course(id,cname_zh)VALUES(?,?)");
for(int i=0;i<=list.size();i++)
{
st2.setInt(1,i);
st2.setString(2, (String)list1.get(i));
st2.addBatch();
st2.executeUpdate();
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
try{
st2.close();
conn.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
public static void main(String args[]) throws SQLException
{
new Dao();
}
}
{
list.add(rs.getString("ID"));
list1.add(rs.getString("name"));
}
关于批量提交,最好是每二十条记录就提交一次!什么原因,仔细想想,应该不难理解!
[email protected]还搞不出来,发邮件给我!
我将代码修改了,中文还是乱码,Access中只有3条记录,插完3条记录,还是会出现下面错误,中文还是乱码
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Zhu.Dao.Mysql(Dao.java:96)
at Zhu.Dao.<init>(Dao.java:25)
at Zhu.Dao.main(Dao.java:124)
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.ArrayList;import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;public class Dao {
private ArrayList list=new ArrayList();
private ArrayList list1=new ArrayList();
private Connection conn;
private Connection conn2;
private Statement st1;
private PreparedStatement st2;
private PreparedStatement st3;
private ResultSet rs;
private String url;
private String url2;
private String user;
private String password;
private String str;
public Dao() throws SQLException
{
Access();
Mysql();
}
public void Access()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
url="jdbc:odbc:test";
try
{
conn=DriverManager.getConnection(url);
st1=conn.createStatement();
rs=st1.executeQuery("select*from aa");
while(rs.next())
{// str=new String(rs.getBytes("name"),"gbk");
list.add(rs.getString("ID"));
list1.add(rs.getString("name"));
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
try{
rs.close();
st1.close();
conn.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
public void Mysql() throws SQLException
{
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
url2="jdbc:mysql://localhost:3306/tt3";
user="root";
password="4691789";
conn2=DriverManager.getConnection(url2,user,password);
conn2.setAutoCommit(false); try{
st2=conn2.prepareStatement("INSERT INTO course(id,cname_zh)VALUES(?,?)");
for(int i=0;i<=list.size();i++)
{
st2.setInt(1,i);
st2.setString(2, (String)list1.get(i));
st2.addBatch();
st2.executeBatch();
conn2.commit();
}
st2.executeUpdate();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
try{
st2.close();
conn.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
public static void main(String args[]) throws SQLException, UnsupportedEncodingException
{
new Dao();
}
}
lz可以试试
乱码的话那就转码吧。
中改成rs.getString(1);
rs.getString(2);试试
还是乱码 不知道怎么改了