jsp 怎么从oracle中取出数据
展开全部
import java.sql.*;
public class DBConnect {
//MySQL的JDBC驱动程序
String DBDriver = "com.mysql.jdbc.Driver";
//设置连接数据库的参数
private String userName = "root";
private String userPwd = "";
//数据库的名字
private String dbName="test";
//连接数据库
private String sConnStr="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPwd;
//数据库连接对象
private Connection conn = null;
//数据库语句对象
public Statement stmt = null;
public ResultSet rs = null;
//建立数据连接函数
public DBConnect() {
try {
//加载数据库驱动程序
Class.forName(DBDriver).newInstance();
//建立连接
conn = DriverManager.getConnection(sConnStr);
//stmt=conn.createStatement();
}
catch (Exception e) {
System.out.println("DBConnect():" + e.getMessage());
}
}
//查询
public ResultSet executeQuery(String sql) {
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.out.println("sql.executeQuery:" + ex.getMessage());
}
return rs;
}
//更新
public boolean executeUpdate(String sql){
try{
stmt = conn.createStatement();
stmt.executeUpdate(sql);
return true;
}catch (SQLException ex) {
System.out.println("sql.executeUpdate:" + ex.getMessage());
return false;
}
}
//关闭数据库的连接
public void close() throws Exception{
conn.close();
stmt.close();
}
}
此代码为数据库的连接及增删查改。
public class DBConnect {
//MySQL的JDBC驱动程序
String DBDriver = "com.mysql.jdbc.Driver";
//设置连接数据库的参数
private String userName = "root";
private String userPwd = "";
//数据库的名字
private String dbName="test";
//连接数据库
private String sConnStr="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPwd;
//数据库连接对象
private Connection conn = null;
//数据库语句对象
public Statement stmt = null;
public ResultSet rs = null;
//建立数据连接函数
public DBConnect() {
try {
//加载数据库驱动程序
Class.forName(DBDriver).newInstance();
//建立连接
conn = DriverManager.getConnection(sConnStr);
//stmt=conn.createStatement();
}
catch (Exception e) {
System.out.println("DBConnect():" + e.getMessage());
}
}
//查询
public ResultSet executeQuery(String sql) {
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.out.println("sql.executeQuery:" + ex.getMessage());
}
return rs;
}
//更新
public boolean executeUpdate(String sql){
try{
stmt = conn.createStatement();
stmt.executeUpdate(sql);
return true;
}catch (SQLException ex) {
System.out.println("sql.executeUpdate:" + ex.getMessage());
return false;
}
}
//关闭数据库的连接
public void close() throws Exception{
conn.close();
stmt.close();
}
}
此代码为数据库的连接及增删查改。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询