package datebase;
import java.sql.*;
public class mypreparedstatement {
private final String db_driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ZC_NmSoft";
public mypreparedstatement()
{
}
public void query() throws SQLException{
Connection conn = this.getconnection();
//*********************************************查询********************
String strsql = "select * from ZC_Attendanceregister where AR_ID = ?";
PreparedStatement pstmtq = conn.prepareStatement(strsql);
pstmtq.setInt(1, 587);
ResultSet rs = pstmtq.executeQuery();
while(rs.next()){
String fname = rs.getString("AR_AdmissionID");
System.out.println("the fname is " + fname);
}
//************************************************删除******************
String strsqld= "delete from ZC_Attendanceregister where AR_ID = ?";
PreparedStatement pstmtd = conn.prepareStatement(strsqld);
pstmtd.setInt(1, 587);
int dd = pstmtd.executeUpdate();
//***********************************************添加*******************
String strsqladd = "insert into ZC_Attendanceregister(AR_RegistrationDate,AR_AdmissionID,AR_Attendance,AR_Notes,AR_Mealssettlement) values(?,?,?,?,?)";
PreparedStatement pstmta = conn.prepareStatement(strsqladd);
pstmta.setString(1, "2010-07-26");
pstmta.setString(2, "260");
pstmta.setString(3, "出勤");
pstmta.setString(4, "备注");
pstmta.setString(5, "应该缴费");
int add = pstmta.executeUpdate();
rs.close();
pstmta.close();
conn.close();
}
private Connection getconnection() throws SQLException{
// class.
Connection conn = null;
try {
Class.forName(db_driver);
conn = DriverManager.getConnection(url,"sa","sa");
}
catch (ClassNotFoundException ex) {}
return conn;
}
//main 测试
public static void main(String[] args) throws SQLException {
mypreparedstatement jdbctest1 = new mypreparedstatement();
jdbctest1.query();
}
}