jsp怎么从数据库中取得已有但未输入的信息
2个回答
2015-05-01
展开全部
<%
//JSP页面直接访问数据库
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("JDBC驱动");
conn = DriverManager.getConnection("url", "username", "password");
stmt = conn.createStatement();
rs = stmt.executeQuery("select factor, ratio from 表名 where id=1");
while(rs.next()){
String factor = rs.getString("factor");
String ratio = rs.getString("ratio");
%>
factor :<%=factor %>
ratio :<%=ratio %>
<%
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
%>
//JSP页面直接访问数据库
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("JDBC驱动");
conn = DriverManager.getConnection("url", "username", "password");
stmt = conn.createStatement();
rs = stmt.executeQuery("select factor, ratio from 表名 where id=1");
while(rs.next()){
String factor = rs.getString("factor");
String ratio = rs.getString("ratio");
%>
factor :<%=factor %>
ratio :<%=ratio %>
<%
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
%>
2016-01-19 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
jsp获取数据已存在但是为输入的信息方法如下:
package Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
private static Connection con = null;
private static Statement statement = null;
private static ResultSet set = null;
private String sql = "";
// 加载SqlServer JDBC驱动
private static String driverNameOfSqlServer = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// IP地址(改为自己的IP地址)
private static String DatabaseIP = "localhost";
// 数据库用户名
private static String DatabaseUser = "sjf";
// 数据库密码
private static String DatabasePassword = "123456";
// 数据库名称
private static String DatabaseName = "pubs";
// URL
private static String DatabaseUrl = "jdbc:sqlserver://" + DatabaseIP + ":1433;DatabaseName = " + DatabaseName;
//获取一个数据库的连接
public Connection getConnection() {
try {
//注册驱动程序
Class.forName(driverNameOfSqlServer);
// 获取连接
con = DriverManager.getConnection(DatabaseUrl, DatabaseUser,DatabasePassword);
} catch (Exception e) {
System.out.println("getConnection出现错误");
e.printStackTrace();
}
return con;
}
//创建会话
public Statement getStatement(Connection con){
if(con != null){
try {
statement = con.createStatement();
return statement;
} catch (SQLException e) {
System.out.println("getStatement出现错误");
e.printStackTrace();
}
}
return null;
}
//查询
public ResultSet getResultSetQuery(Statement statement,String sql) {
if(statement != null){
try {
set = statement.executeQuery(sql);
return set;
} catch (SQLException e) {
System.out.println("getResultSetQuery出现错误");
e.printStackTrace();
}
}
return null;
}
//增加,修改,删除记录
public void getResultSetUpdate(Statement statement,String sql) {
if(statement != null){
try {
statement.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("getResultSetUpdate出现错误");
e.printStackTrace();
}
}
}
//关闭连接
public static void colse(Connection con){
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭会话
public static void close(Statement statement){
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭查询集
public static void close(ResultSet set){
if(set != null){
try {
set.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
DB db = new DB();
Connection con = db.getConnection();
Statement statement = db.getStatement(con);
String sql = "select * from dbo.jobs";
ResultSet rs = db.getResultSetQuery(statement, sql);
try {
if(rs.next()){
System.out.println("fdfsdfsdff"+rs.getString("job_desc"));
}
} catch (SQLException e) {
e.printStackTrace();
}
package Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
private static Connection con = null;
private static Statement statement = null;
private static ResultSet set = null;
private String sql = "";
// 加载SqlServer JDBC驱动
private static String driverNameOfSqlServer = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// IP地址(改为自己的IP地址)
private static String DatabaseIP = "localhost";
// 数据库用户名
private static String DatabaseUser = "sjf";
// 数据库密码
private static String DatabasePassword = "123456";
// 数据库名称
private static String DatabaseName = "pubs";
// URL
private static String DatabaseUrl = "jdbc:sqlserver://" + DatabaseIP + ":1433;DatabaseName = " + DatabaseName;
//获取一个数据库的连接
public Connection getConnection() {
try {
//注册驱动程序
Class.forName(driverNameOfSqlServer);
// 获取连接
con = DriverManager.getConnection(DatabaseUrl, DatabaseUser,DatabasePassword);
} catch (Exception e) {
System.out.println("getConnection出现错误");
e.printStackTrace();
}
return con;
}
//创建会话
public Statement getStatement(Connection con){
if(con != null){
try {
statement = con.createStatement();
return statement;
} catch (SQLException e) {
System.out.println("getStatement出现错误");
e.printStackTrace();
}
}
return null;
}
//查询
public ResultSet getResultSetQuery(Statement statement,String sql) {
if(statement != null){
try {
set = statement.executeQuery(sql);
return set;
} catch (SQLException e) {
System.out.println("getResultSetQuery出现错误");
e.printStackTrace();
}
}
return null;
}
//增加,修改,删除记录
public void getResultSetUpdate(Statement statement,String sql) {
if(statement != null){
try {
statement.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("getResultSetUpdate出现错误");
e.printStackTrace();
}
}
}
//关闭连接
public static void colse(Connection con){
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭会话
public static void close(Statement statement){
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭查询集
public static void close(ResultSet set){
if(set != null){
try {
set.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
DB db = new DB();
Connection con = db.getConnection();
Statement statement = db.getStatement(con);
String sql = "select * from dbo.jobs";
ResultSet rs = db.getResultSetQuery(statement, sql);
try {
if(rs.next()){
System.out.println("fdfsdfsdff"+rs.getString("job_desc"));
}
} catch (SQLException e) {
e.printStackTrace();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询