java database operation problems

Category: Java SE
 
foot2014
2009-10-19 12:28:51

Sponsored Links
Using java language in Access data into Mysql in, there is always Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 error, And only import a record, but the imported Chinese is garbled, what causes it, consult experts!
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 ();
}

}

Sponsored Links

java_bridge
2009-10-19 12:43:48
if (rs.next ()) / / this position should use cycle. Like you do, it is certainly only a record! About the garbage problem, a variety of situations, very complicated!
{
list.add (rs.getString ("ID"));
list1.add (rs.getString ("name"));
}
on the bulk submission, preferably every twenty record is submitted once! Whatever reason, think about it, should not be difficult to understand!
[email protected] still do not come out, email me!
wgfysl
2009-10-19 12:55:55
if (rs.next ()) -> while (rs.next ())

garbled words, the project and db encoding used in agreement, Access should be the encoding system encoding it (not used Access)

wahahazlt2
2009-10-19 01:13:26

I modified the code, Chinese or distortion, Access, only three record, finished three records inserted, or the following error will appear, Chinese or distortion
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 ();
}

}

luoyingsp
2009-10-19 01:20:45
request expert advice, thank you!
chaobase
2009-10-19 01:33:22
for (int i = 0; i <= list.size (); i + +) is not should i <= list.size () -1 ah. .

afxma
2009-10-19 01:36:12
Well this problem has been solved yesterday, but Chinese garbage problem still does not know how to do
lizhenan2009
2009-10-19 01:47:13
Check your database encoding, because when you install mysql did not change encoding.
lz can try
vennloh
2009-10-19 02:02:39
if (rs.next ()) -> while (rs.next ())

this place is certainly to be changed.
garbled, then it would transcode it.
xiancaonima
2009-10-19 02:18:57


the change rs.getString (1);
rs.getString (2); Try
dengjiangbo123
2009-10-19 02:38:46
has list1.add (rs.getString ("name")) -> list1.add (new String (rs.getBytes ("name"), "gbk"))
do not know how to change or distortion the
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.