public class KetNoi {
public static String ServerConnectString = "jdbc:sqlserver://HOANGKIM;databaseName=newsonline";
}
String ServerConnectString=KetNoi.ServerConnectString;
câu lệnh sececl trong java
public ArrayList<news> loadavatar(){
ArrayList<news> list = new ArrayList<news>();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(ServerConnectString, "sa", "sa");
PreparedStatement comm = con.prepareStatement("select image from employee where username=?");
ResultSet rs = comm.executeQuery();
while (rs.next()) {
news ns = new news();
ns.setImage(rs.getString("image"));
list.add(ns);
}
con.close();
} catch (Exception e) {
System.out.println("err" + e.toString());
}
return list;
}
update trong java có sữ dụng transaction
public String getdelete(Object idss) {
int id = (int) idss;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(ServerConnectString, "sa", "sa");
PreparedStatement comm = con.prepareStatement("BEGIN TRY\n"
+ " BEGIN TRAN\n"
+ " delete from dbo.comment where newsid=?\n"
+ " delete from dbo.news where newsid=?\n"
+ " COMMIT TRAN\n"
+ "END TRY\n"
+ "\n"
+ "BEGIN CATCH\n"
+ " ROLLBACK TRAN\n"
+ "END CATCH");
comm.setInt(1, id);
comm.setInt(2, id);
comm.executeUpdate();
con.close();
} catch (Exception e) {
return "loi";
}
return "list";
}
Update trong java
public String updateappoval() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(ServerConnectString, "sa", "sa");
PreparedStatement comm = con.prepareStatement("UPDATE news SET date_approval='Yes' WHERE newsID=?");
comm.setInt(1, tinchitiet.newsid);
comm.executeUpdate();
con.close();
} catch (Exception e) {
}
return "list";
}
select có sữ dụng join 2 bảng
public ArrayList<news> getdata() {
ArrayList<news> list = new ArrayList<news>();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(ServerConnectString, "sa", "sa");
PreparedStatement comm = con.prepareStatement("select n.newsid,n.title,n.contents,n.date_post,n.username,z.name,n.date_approval from news n join zone z on n.zoneID = z.zoneID");
ResultSet rs = comm.executeQuery();
while (rs.next()) {
news ns = new news();
ns.setNewsid(rs.getInt(1));
ns.setTitle(rs.getString(2));
ns.setUsername(rs.getString(5));
ns.setDate_post(rs.getString(4));
ns.setContents(rs.getString(3));
ns.setName(rs.getString(6));
ns.setDate_approval(rs.getString(7));
list.add(ns);
}
con.close();
} catch (Exception e) {
System.out.println("err" + e.toString());
}
return list;
}
inser trong java
public void addComment(){
getconnection();
sql = "insert into comment(contents,name) values(?,?)";
try {
stm = conn.prepareStatement(sql);
stm.setString(1,this.getContents());
stm.setString(2, this.getName());
int executeUpdate = stm.executeUpdate();
if(executeUpdate > 0){
System.out.println("Add comment successFully");
}
} catch (Exception ex) {
System.out.println("err:"+ex.toString());
Logger.getLogger(UserManagedBean.class.getName()).log(Level.SEVERE, null, ex);
}finally {
closeAll(conn, stm, rs);
}
}
public static String ServerConnectString = "jdbc:sqlserver://HOANGKIM;databaseName=newsonline";
}
câu lệnh sececl trong java
public ArrayList<news> loadavatar(){
ArrayList<news> list = new ArrayList<news>();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(ServerConnectString, "sa", "sa");
PreparedStatement comm = con.prepareStatement("select image from employee where username=?");
ResultSet rs = comm.executeQuery();
while (rs.next()) {
news ns = new news();
ns.setImage(rs.getString("image"));
list.add(ns);
}
con.close();
} catch (Exception e) {
System.out.println("err" + e.toString());
}
return list;
}
update trong java có sữ dụng transaction
public String getdelete(Object idss) {
int id = (int) idss;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(ServerConnectString, "sa", "sa");
PreparedStatement comm = con.prepareStatement("BEGIN TRY\n"
+ " BEGIN TRAN\n"
+ " delete from dbo.comment where newsid=?\n"
+ " delete from dbo.news where newsid=?\n"
+ " COMMIT TRAN\n"
+ "END TRY\n"
+ "\n"
+ "BEGIN CATCH\n"
+ " ROLLBACK TRAN\n"
+ "END CATCH");
comm.setInt(1, id);
comm.setInt(2, id);
comm.executeUpdate();
con.close();
} catch (Exception e) {
return "loi";
}
return "list";
}
Update trong java
public String updateappoval() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(ServerConnectString, "sa", "sa");
PreparedStatement comm = con.prepareStatement("UPDATE news SET date_approval='Yes' WHERE newsID=?");
comm.setInt(1, tinchitiet.newsid);
comm.executeUpdate();
con.close();
} catch (Exception e) {
}
return "list";
}
select có sữ dụng join 2 bảng
public ArrayList<news> getdata() {
ArrayList<news> list = new ArrayList<news>();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(ServerConnectString, "sa", "sa");
PreparedStatement comm = con.prepareStatement("select n.newsid,n.title,n.contents,n.date_post,n.username,z.name,n.date_approval from news n join zone z on n.zoneID = z.zoneID");
ResultSet rs = comm.executeQuery();
while (rs.next()) {
news ns = new news();
ns.setNewsid(rs.getInt(1));
ns.setTitle(rs.getString(2));
ns.setUsername(rs.getString(5));
ns.setDate_post(rs.getString(4));
ns.setContents(rs.getString(3));
ns.setName(rs.getString(6));
ns.setDate_approval(rs.getString(7));
list.add(ns);
}
con.close();
} catch (Exception e) {
System.out.println("err" + e.toString());
}
return list;
}
inser trong java
public void addComment(){
getconnection();
sql = "insert into comment(contents,name) values(?,?)";
try {
stm = conn.prepareStatement(sql);
stm.setString(1,this.getContents());
stm.setString(2, this.getName());
int executeUpdate = stm.executeUpdate();
if(executeUpdate > 0){
System.out.println("Add comment successFully");
}
} catch (Exception ex) {
System.out.println("err:"+ex.toString());
Logger.getLogger(UserManagedBean.class.getName()).log(Level.SEVERE, null, ex);
}finally {
closeAll(conn, stm, rs);
}
}