用JDBC技术\x0d\创建数据库连接,分为以下几步:\x0d\1装载并注册数据库的JDBC驱动程序\x0d\2取得数据库连接\x0d\3建立Statement 对象\x0d\4准备并执行调用SQL语句\x0d\5处理ResultSet中的记录集\x0d\6释放资源\x0d\第一步\x0d\加载驱动程序\x0d\ try{ //装载MySQL数据库驱动\x0d\ ClassforName("commysqljdbcDriver");\x0d\ }\x0d\ catch(ClassNotFoundException e) \x0d\ { \x0d\ eprintStackTrace();\x0d\ }\x0d\注意:在使用JDBC之前,要在文件前导入有关SQL的类即\x0d\ import javasql\x0d\第二步\x0d\取得数据库连接\x0d\try{\x0d\String url="jdbc:mysql://localhost:3306/student;\x0d\String user="root";\x0d\String password="1234";\x0d\con=DriverManagergetConnection(url,user,password);\x0d\}\x0d\catch(SQLException e)\x0d\{\x0d\ eprintStackTrace();\x0d\ }\x0d\第三步\x0d\建立Statement 对象\x0d\try{\x0d\ Statement sql=concreateStatement();\x0d\ }\x0d\catch(SQLException e)\x0d\ {\x0d\ eprintStackTrace();\x0d\ }\x0d\第四步\x0d\执行各种SQL语句\x0d\try{\x0d\ ResultSet rs=sqlexecuteQuery(\x0d\ "select from student");\x0d\ }\x0d\catch(SQLException e)\x0d\ {\x0d\ eprintStackTrace();\x0d\ }\x0d\第五步\x0d\获取查询结果\x0d\ ResultSet rs=sqlexecuteQuery(\x0d\ "select from student");\x0d\ while(rsnext())\x0d\ {\x0d\ rsgetString(2)或者是rsgetString("name");\x0d\ rsgetInt(3)或者是rsgetInt("age");\x0d\ }\x0d\注意\x0d\只有select语句才会有结果集返回;\x0d\ResultSet对象一次只能看到一个数据行\x0d\使用next()方法走到下一数据行\x0d\获得一行数据后,ResultSet对象可以使用getXxx()方法获得字段值,将位置索引或字段名传递给get第六步\x0d\关闭创建的各个对象(后打开的先关)\x0d\ rsclose();\x0d\ sqlclose();\x0d\ conclose();Xxx方法()即可。
protected static Connection con = null;
protected static Statement stmt = null;
protected static ResultSet rs = null;
protected void Select(String url, String user, String password, String sql) {
try {
String className = "oraclejdbcdriverOracleDriver";
ClassforName(className);
if (urlequals(null) || userequals(null) || passwordequals(null)) {
Systemoutprintln("连接创建失败!请传入连接所需参数:url,user,password!");
} else {
con = DriverManagergetConnection(url, user, password);
stmt = concreateStatement();
rs = stmtexecuteQuery(sql);
}
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
Systemoutprintln("导包失败");
}
}
在<% %>小脚本里面创建对象 然后调用这个方法 传入参数
Select("jdbc:oracle:thin:@localhost:1521:XE", "数据库账号",
"数据库密码", "Sql命令"); 大概这样
对了 别忘了构建jar包
加载驱动 新建连接 建立状态 *** 作 update close
<%
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
boolean flag=false;
%>
<%
String UID=requestgetParameter("UID");
String psw=requestgetParameter("psw");
try{
ClassforName(DBDRIVER);
conn=DriverManagergetConnection(DBURL,DBUSER,DBPSW);
String sql="SELECT UID,psw FROM student WHERE UID= AND psw=";
pstmt=connprepareStatement(sql);
pstmtsetString(1,UID);
pstmtsetString(2,psw);
rs=pstmtexecuteQuery();
if(rsnext()){
flag=true;
}
}catch(Exception e){ outprintln(etoString());
}finally{
try{
connclose();
}catch(Exception e){ outprintln(etoString());
}
}
请问下是什么结构?用jdbc连接的话:
public class DBUtil {
private static String user;
private static String password;
private static String url;
static{
Properties prop=new Properties();
try {
ClassLoader classLoader=DBUtilclassgetClassLoader();
InputStream is=classLoadergetResourceAsStream("dbproperties");
propload(is);
user=propgetProperty("user");
password=propgetProperty("password");
url=propgetProperty("url");
ClassforName("commysqljdbcDriver");
} catch (Exception e) {
eprintStackTrace();
throw new RuntimeException("找不到加载类");
}
}
public static Connection getConnection()throws Exception{
Connection conn=null;
conn=DriverManagergetConnection(url,user,password);
return conn;
}
public static void close(Connection conn){
if(conn!=null){
try {
connclose();
} catch (SQLException e) {
eprintStackTrace();
}
}
}
public static void main(String[] args)throws Exception {
Systemoutprintln(DBUtilgetConnection());
}
}
如果是用SSH架构的话,用hibernate里面去配置就OK了!
首先是数据库连接代码类:
然后在你的jsp页面写上调用数据连接类的增删改查就可以了。
不懂hi我
jsp页面中
<%
String sqlgetServiceId="select eid from eip_service e where eservice_name_en='"+serviceNamesubstring(serviceNamelastIndexOf("_")+1)+"' and eservice_version=10";
int sid=BaseDBqueryId(sqlgetServiceId, null);
%>
BaseDBjava
import javasqlConnection;
import javasqlDriverManager;
import javasqlPreparedStatement;
import javasqlResultSet;
import javasqlSQLException;
import javautilregexMatcher;
import javautilregexPattern;
public class BaseDB {
public static String URL = "jdbc:oracle:thin:@192168174189:1521:soadb"; //版本管理ERP数据库配置
public static String NAME = "SVMDEV";//用户名
public static String PWD = "SVMPWD";//密码
public static PreparedStatement ps =null;
public static ResultSet rs =null;
public static Connection connection=null;
//获取数据库连接信息
public static Connection getConnection() {
try {
ClassforName("oraclejdbcOracleDriver");
if (connection==null) {
connection=DriverManagergetConnection(URL, NAME, PWD);
}
} catch (ClassNotFoundException e) {
eprintStackTrace();
} catch (SQLException e) {
eprintStackTrace();
}
return connection;
}
//查询数据,根据相关信息查询得到当前服务的某个需要的id
public static int queryId(String sql,String parameter[] ) {
int getId=0;
try {
connection=getConnection();
ps=connectionprepareStatement(sql);
if (parameter!=null) {
for (int i = 1; i <=parameterlength; i++) {
pssetString(i,parameter[i-1]);
}
}
rs=psexecuteQuery();
if(rsnext()&&rs!=null){
getId=rsgetInt(1);
}
} catch (SQLException e) {
eprintStackTrace();
}finally{
closeAll(ps, rs, connection);
}
return getId;
}
//修改数据
public static int updateData(String sql,String parameter[] ) {
int count=0;
try {
connection=getConnection();
ps=connectionprepareStatement(sql);
if (parameter!=null) {
for (int i = 1; i <=parameterlength; i++) {
pssetString(i,parameter[i-1]);
}
}
count=psexecuteUpdate();
} catch (SQLException e) {
eprintStackTrace();
}finally{
closeAll(ps, rs, connection);
}
return count;
}
//插入数据
public static int insertData(String sql,String parameter[]) {
int num=0;
try {
connection=getConnection();
ps=connectionprepareStatement(sql);
if (parameter!=null) {
for (int i = 0; i <parameterlength; i++) {
pssetString(i+1,parameter[i]);
}
}
num=psexecuteUpdate();
} catch (Exception e) {
eprintStackTrace();
}finally{
closeAll(ps,null,connection);
}
return num;
}
//关闭所有
public static void closeAll(PreparedStatement ps,ResultSet rs,Connection connection) {
try {
if (ps!=null) {
psclose();
}
} catch (Exception e2) {
try {
if (rs!=null) {
rsclose();
rs=null;
}
} catch (Exception e3) {
try {
if (connection!=null) {
//connectionclose();
//connection=null;
}
} catch (Exception e4) {
e4printStackTrace();
}
}
}
}
}
HTML是固定死的,无法动态的显示处理后的结果。
而servlet和jsp解决了这个问题,servlet可以接受来自用户登陆请求页面的数据,进行计算处理之后,返回给jsp页面来显示。
就你这个问题来说,应该这样写:
登录页面使用html和jsp都无所谓,里面需要一个表单FORM,action="处理业务的servlet的url",有一个提交查询按钮,这个查询按钮为submit。那么点击查询后,会把表单数据提交给servlet,在servlet中可以通过requestgetParameter("参数名");来获取表单中的用户名参数,然后在servlet中调用jdbc代码取得数据库中对应用户的余额,然后通过requestsetAttribute("余额",yue);可以传给jsp,在jsp页面中使用<%=requestgetAttribute("余额")%>来显示相应的余额。
JSP连接SQL数据库实现查找(支持模糊查找,查找年龄段),插入信息<实例>
<h2>学生信息查询</h2>
<form method="POST" action="Namejsp">
<h4>按姓名查找(支持模糊查询)</h4>
<table bgcolor="#CCCCCC">
<tr>
<td>查找姓名</td>
<td><input type="text" name="name" size="15" /></td>
<td><input type="submit" value="查找"></td>
</tr>
</table>
</form>
<br/>
<form method="POST" action="Agejsp">
<h4>按年龄查找</h4>
<table border="1" bgcolor="#CCCCCC">
<tr>
<td>查找年龄</td>
<td><input type="text" name="agemin" size="5" /></td>
<td>到</td>
<td><input type="text" name="agemax" size="5" /></td>
<td><input type="submit" value="查找"></td>
</tr>
</table>
</form>
<form action="Insertjsp" method="POST">
<h4>插入信息到表中</h4>
<table border="1" bgcolor="#cccccc">
<tr>
<td>姓名</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>性别</td>
<td><input type="text" name="sex" /></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td>系别</td>
<td><input type="text" name="dept" /></td>
</tr>
<tr>
<td><input type="submit" value="插入" /></td>
<td><input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</center>
</body>
</html>
以上就是关于在jsp编程中如何连接数据库全部的内容,包括:在jsp编程中如何连接数据库、如何用JSP修改数据库内容、网站的jsp页面怎么连接数据库啊等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)