当前位置: 技术问答>java相关
在JB中怎么调用sql语句?
来源: 互联网 发布时间:2017-03-11
本文导语: 用什么控件?增加、删除、修改操作,sql语句会写,但是不知怎么调用,最好有例子 | Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为你的...
用什么控件?增加、删除、修改操作,sql语句会写,但是不知怎么调用,最好有例子
|
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为你的数据库的SID
String user="scott";
String password="tiger";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
out.println(rs.getString(1));
out.println(rs.getString(2));
}
out.print("数据库操作成功,恭喜你");
rs.close();
stmt.close();
conn.close();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为你的数据库的SID
String user="scott";
String password="tiger";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
out.println(rs.getString(1));
out.println(rs.getString(2));
}
out.print("数据库操作成功,恭喜你");
rs.close();
stmt.close();
conn.close();
|
public String getSeqPlanId(){
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = dataSource.getConnection();
String sql = "select SEQ_PROJECT_PLAN.nextval from dual";
pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
rs.next();
String seq = rs.getString(1);
return seq;
} catch(SQLException e){
log("getSeqPlanId",e.toString());
throw new EJBException(e);
}finally{
if(pstmt != null){
try{
pstmt.close();
}catch(Exception ex){
}
}
if(conn != null){
try{
conn.close();
}catch(Exception ex){
}
}
} // end of finally
};
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = dataSource.getConnection();
String sql = "select SEQ_PROJECT_PLAN.nextval from dual";
pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
rs.next();
String seq = rs.getString(1);
return seq;
} catch(SQLException e){
log("getSeqPlanId",e.toString());
throw new EJBException(e);
}finally{
if(pstmt != null){
try{
pstmt.close();
}catch(Exception ex){
}
}
if(conn != null){
try{
conn.close();
}catch(Exception ex){
}
}
} // end of finally
};