java中 向数据库 插入数据项?

例如向数据库 “教授”的表中,插入一个数据项,数据项有:编号,姓名,职称,学院

//先连接好数据源,假设连接成功并取名为users,无密码和用户名
//下简单的对数据库中users表进行了添加和修改,编译执行无误
import java.sql.*;

public class operateDateBase {

public static void main(String[] args) {

Connection ct=null;
PreparedStatement ps=null;
int a=0,b=0;
try {

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为数据库的SID
String user="test";
String password="test";
ct= DriverManager.getConnection(url,user,password);

//下为插入语句,values()中各值为professors 表中各字段的值
ps=ct.prepareStatement("insert into professors ( no, name,workname, xueyuan) values( ?, ?,?, ?)");
ps.setInt(1,2);
ps.setString(2,"232");

ps.setString(3,"2");

ps.setString(4,"xxx");

b=ps.executeUpdate();

//如果操作成功,a和b的值都应为1
System.out.println("a="+a+" b="+b);

} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(ps!=null)
{
ps.close();
ps=null;
}
if(ct!=null)
{
ct.close();
ct=null;
}
} catch (Exception e2) {
e2.printStackTrace();
}

}
}

}22| 评论(1)
向TA求助
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-08-21
你是想向教授这个表中插入一条数据么
是什么数据库呀,ORACLE还是SQL SERVER还是什么别的。
相似回答