首页上一页 1 下一页尾页 3 条记录 1/1页
求助《java入门到精通》中第20章第5各实例,相关问题。
发表在Java图书答疑
2009-01-01
是否精华
是
否
版块置顶:
是
否
import java.io.*;
import java.sql.*;
public class tc {
static Connection con;
static PreparedStatement sql;
static ResultSet res;
public Connection getConnection() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;"
+ "DatabaseName=Stock_manage", "sa", "23572306");
System.out.println("数据库链接成功");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
tc c = new tc();
con = c.getConnection();
//查询数据库原来信息
try {
sql = con.prepareStatement("select * from student");
res = sql.executeQuery();
System.out.println("原数据");
while(res.next()){
String id = res.getString(1);
String name = res.getString("name");
String sex = res.getString("sex");
String brithday = res.getString("brithday");
System.out.print("编号" + id);
System.out.print("名字" + name);
System.out.print("性别" + sex);
System.out.println("生日" + brithday);
}
//添加数据
sql = con.prepareStatement("insert into student values(?,?,?,?)");
sql.setInt(1, 10);
sql.setString(2, "张宏");
sql.setString(3,"女");
sql.setString(4, "1993-10-20");
sql.executeUpdate();
//修改数据
sql = con.prepareStatement("update student set brithday = ? where id = ?");
sql.setString(1, "1994-11-05");
sql.setInt(2, 3);
sql.executeUpdate();
//删除数据
sql = con.prepareStatement("delete from student where id = ?");
sql.setInt(1,5);
sql.executeUpdate();
//查询更新后的数据
sql = con.prepareStatement("select * from student");
res = sql.executeQuery();
System.out.println("更新后");
while (res.next()) {
String id = res.getString(1);
String name = res.getString("name");
String sex = res.getString("sex");
String brithday = res.getString("brithday");
System.out.print("编号" + id);
System.out.print("名字"+ name);
System.out.print("性别" + sex);
System.out.println("生日" + brithday);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
请问在修改数据,那段代码中的sql = con.prepareStatement("update student set brithday = ? where id = ?");
sql.setString(1, "1994-11-05");
sql.setInt(2, 3);
其中sql.setString(1, "1994-11-05");中的数字1代表什么?
sql.setInt(2, 3);中的数字2代码数据库表中的数字2和3又代表什么?具体是代表数据库表中的那一列?
备注:数据库表中的各字段名称依次是:id; name;sex;birthday
import java.sql.*;
public class tc {
static Connection con;
static PreparedStatement sql;
static ResultSet res;
public Connection getConnection() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;"
+ "DatabaseName=Stock_manage", "sa", "23572306");
System.out.println("数据库链接成功");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
tc c = new tc();
con = c.getConnection();
//查询数据库原来信息
try {
sql = con.prepareStatement("select * from student");
res = sql.executeQuery();
System.out.println("原数据");
while(res.next()){
String id = res.getString(1);
String name = res.getString("name");
String sex = res.getString("sex");
String brithday = res.getString("brithday");
System.out.print("编号" + id);
System.out.print("名字" + name);
System.out.print("性别" + sex);
System.out.println("生日" + brithday);
}
//添加数据
sql = con.prepareStatement("insert into student values(?,?,?,?)");
sql.setInt(1, 10);
sql.setString(2, "张宏");
sql.setString(3,"女");
sql.setString(4, "1993-10-20");
sql.executeUpdate();
//修改数据
sql = con.prepareStatement("update student set brithday = ? where id = ?");
sql.setString(1, "1994-11-05");
sql.setInt(2, 3);
sql.executeUpdate();
//删除数据
sql = con.prepareStatement("delete from student where id = ?");
sql.setInt(1,5);
sql.executeUpdate();
//查询更新后的数据
sql = con.prepareStatement("select * from student");
res = sql.executeQuery();
System.out.println("更新后");
while (res.next()) {
String id = res.getString(1);
String name = res.getString("name");
String sex = res.getString("sex");
String brithday = res.getString("brithday");
System.out.print("编号" + id);
System.out.print("名字"+ name);
System.out.print("性别" + sex);
System.out.println("生日" + brithday);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
请问在修改数据,那段代码中的sql = con.prepareStatement("update student set brithday = ? where id = ?");
sql.setString(1, "1994-11-05");
sql.setInt(2, 3);
其中sql.setString(1, "1994-11-05");中的数字1代表什么?
sql.setInt(2, 3);中的数字2代码数据库表中的数字2和3又代表什么?具体是代表数据库表中的那一列?
备注:数据库表中的各字段名称依次是:id; name;sex;birthday