向表里追加数据怎样写脚本,各位高手指点1-2,
例子::
数据库表
//表:products 列名 数据类型 长度 suppliers 列名 数据类型 长度
// pid int 4 sid int 4
// pname varchar 200 sname varchar 100
// pprice float 8 sregion char 50
// pcolor char 10 sdescription varchar 250
// 表: products_suppliers 列名 数据类型 长度
// pid int 4
// sid int 4
// Quantity int 4
import java.util.*;
import java.sql.*;
class DBAccessor
{
Connection conn;
PreparedStatement ipStmt,rpStmt;
//构造器
DBAccessor() throw SQLException,ClassNotFoundException
{
String dbURL = "jdbc:odbc:Sample";
//装入JdbcOdbc驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//建立与数据库的连接
conn = DriverManager.getConnection(dbURL,"sa","123");
//建立插入数据的预处理语句
ipStmt = conn.prepareStatement("INSERT INTO suppliers(sid,sname,sregion,sdescriptin) VALUES(?,?,?,?)");
//建立检索数据的预处理语句
rpStmt = conn.prepareStatement("SELECT * FROM suppliers WHERE sregion = ?");
}
//插入数据的方法
void insertDATA() throw SQLException
{
int sids[] = {200,201,202,203,204},i = 0;
String snames[] = {"Tom", "Jerry", "Dilbert", "Calvin", "Hobbes"};
String sregions[] = {"London", "Mumbai", "New York", "London", "London"};
String sdes[] = {"Pen dealer", "Paper dealer", "Clock dealer", "Pencil dealer"," Pen Paper dealer"}
for(i = 0;i <5;i++)
{
ipStmt.setInt(1,sids[i]);
ipStmt.setString(2,sids[i]);
ipStmt.seString(3,sids[i]);
ipStmt.seString(4,sids[i]);
ipStmt.execute();
}
ipStmt.close();
}
//检索数据的方法
void retrieveData() throw SQLException
{
SesyltSet rs;
rpStmt.setString(1,"London");
rs = rpStmt.executeQuery();
System.out.println("Suppliers table ...\n");
while (rs.next())
{
System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
}
rs.close();
rpStmt.close();
}
};
//主类
class DBPSTester
{
public static void main(Striing[] args)
{
try{
DBAccessor dba = new DBAccessor();
dba.insertData();
}
catch(SQLException e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
也许会有点帮助!!
[此贴子已经被作者于2005-12-20 21:23:17编辑过]