huangbb12345
   
發文: 19
積分: 0
|
於 2014-02-06 23:50
     
我的程式碼如下所示, 我想把test.txt檔裡的資料存進資料庫裡 txt檔的部分內容如下: 1001, <1,88069>, 287 10017, <1,88069>, 136 1002, <1,88069>, 98 1003, <1,88069>, 387 10033, <87070,88069>,1 10036, <87070,88069>,1 1004, <1,88069>, 1102 分別想把第一欄位存成資料庫中的item,第二欄位存成lifetime,第三欄位存成count, 但我執行程式時,跑出下面的error,請問要如何修改呢? 謝謝各位的幫忙!! InsertDB Exception :java.sql.SQLException: Parameter index out of range (0 < 1 ). Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at db.jdbcmysql2.main(jdbcmysql2.java:185)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| package db;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class jdbcmysql2
{
private Connection con = null;
private Statement stat = null;
private ResultSet rs = null;
private PreparedStatement pst = null;
private String dropdbSQL = "DROP TABLE udp2 ";
private String createdbSQL = "CREATE TABLE udp2 ("+"item VARCHAR(15)"+",lifetime VARCHAR(255)"+",count INTEGER)";
private String insertdbSQL = "insert into udp2(item,lifetime,count) VALUES(?,?,?) from udp2 ";
private String selectSQL = "select * from udp2 ";
public jdbcmysql2()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost/udp?useUnicode=true&characterEncoding=Big5","root","test");
}
catch(ClassNotFoundException e)
{
System.out.println("DriverClassNotFound :"+e.toString());
}
catch(SQLException x)
{
System.out.println("Exception :"+x.toString());
}
}
public void createTable()
{
try
{
stat = con.createStatement();
stat.executeUpdate(createdbSQL);
}
catch(SQLException e)
{
System.out.println("CreateDB Exception :" + e.toString());
}
finally
{
Close();
}
}
public void insertTable(String item, String lifetime, String count)
{
try
{
pst = con.prepareStatement(insertdbSQL);
pst.setString(0, item);
pst.setString(1, lifetime);
pst.setString(2, count);
pst.executeUpdate();
}
catch(SQLException e)
{
System.out.println("InsertDB Exception :" + e.toString());
}
finally
{
Close();
}
}
public void dropTable()
{
try
{
stat = con.createStatement();
stat.executeUpdate(dropdbSQL);
}
catch(SQLException e)
{
System.out.println("DropDB Exception :" + e.toString());
}
finally
{
Close();
}
}
public void SelectTable()
{
try
{
stat = con.createStatement();
rs = stat.executeQuery(selectSQL);
System.out.println("Item\t\tLifetime\t\tCount");
while(rs.next())
{
System.out.println(rs.getInt("item")+"\t\t"+
rs.getString("lifetime")+"\t\t"+ rs.getString("count"));
}
}
catch(SQLException e)
{
System.out.println("DropDB Exception :" + e.toString());
}
finally
{
Close();
}
}
private void Close()
{
try
{
if(rs!=null)
{
rs.close();
rs = null;
}
if(stat!=null)
{
stat.close();
stat = null;
}
if(pst!=null)
{
pst.close();
pst = null;
}
}
catch(SQLException e)
{
System.out.println("Close Exception :" + e.toString());
}
}
public static void main(String[] args) throws IOException
{
jdbcmysql2 test = new jdbcmysql2();
test.dropTable();
test.createTable();
Scanner scan = new Scanner(new File("test.txt"));
while (scan.hasNext())
{
String perLine = scan.nextLine().trim();
String[] dataAry = perLine.split(", ");
test.insertTable(dataAry[0], dataAry[1], dataAry[2]);
test.SelectTable();
}
}
}
|
  |