hibernate HQL 更新⼀删除问题

2024-12-31 12:58:24
推荐回答(3个)
回答1:

Session session = HibernateSessionFactory.getSession();
String hql = "update Exam set endTime=? where id=?";
Query q = session.createQuery(hql);
q.setTimestamp(0, endTime);
q.setLong(1, examId);

Transaction t = session.beginTransaction();
q.executeUpdate();
t.commit();
有些地方需要改下,代码很完整!

回答2:

以前的时候自己用的
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DB {

private String username = "root";
private String password = "mysql";
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://127.0.0.1/shop";

public Connection conn=null;
public Statement statement=null;
public ResultSet rs=null;

public Connection getConnection() {
if(conn!=null)
return conn;
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url, username, password);

} catch (SQLException e) {
System.out.println("");
e.printStackTrace();
}
return conn;
}

public PreparedStatement prepare(String sql){
PreparedStatement ps=null;
try {
Connection conn = this.getConnection();
ps=conn.prepareStatement(sql);
//System.out.println("")
} catch (SQLException e) {
}
return ps;
}

public ResultSet exeQuery(String sql) {
ResultSet rs = null;
Connection conn = this.getConnection();
try {
statement=conn.createStatement();
rs=statement.executeQuery(sql);
} catch (SQLException e) {
System.out.println" + sql);
e.printStackTrace();
}
return rs;
}

public int exeOther(String sql){
int count=0;
conn = this.getConnection();
try {
statement=conn.createStatement();
count=statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}

return count;
}

public void close(){
try {
if(rs!=null)
rs.close();
if(statement!=null)
statement.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

回答3:

你在百度搜 四湖论坛 希望对你有帮助