在jsp编程中如何连接数据库?

在jsp编程中如何连接数据库?,第1张

用JDBC技术\x0d\x0a创建数据库连接,分为以下几步:\x0d\x0a1.装载并注册数据库的JDBC驱动程序\x0d\x0a2.取得数据库连接\x0d\x0a3.建立Statement 对象\x0d\x0a4.准备并执行调用SQL语句\x0d\x0a5.处理ResultSet中的记录集\x0d\x0a6.释放资源\x0d\x0a第一步\x0d\x0a加载驱动程序\x0d\x0a try{ //装载MySQL数据库驱动\x0d\x0a Class.forName("com.mysql.jdbc.Driver")\x0d\x0a }\x0d\x0a catch(ClassNotFoundException e) \x0d\x0a { \x0d\x0a e.printStackTrace()\x0d\x0a }\x0d\x0a注意:在使用JDBC之前,要在文件前导入有关SQL的类即\x0d\x0a import java.sql.*\x0d\x0a第二步\x0d\x0a取得数据库连接\x0d\x0atry{\x0d\x0aString url="jdbc:mysql://localhost:3306/student\x0d\x0aString user="root"\x0d\x0aString password="1234"\x0d\x0acon=DriverManager.getConnection(url,user,password)\x0d\x0a}\x0d\x0acatch(SQLException e)\x0d\x0a{\x0d\x0a e.printStackTrace()\x0d\x0a }\x0d\x0a第三步\x0d\x0a建立Statement 对象\x0d\x0atry{\x0d\x0a Statement sql=con.createStatement()\x0d\x0a }\x0d\x0acatch(SQLException e)\x0d\x0a {\x0d\x0a e.printStackTrace()\x0d\x0a}\x0d\x0a第四步\x0d\x0a执行各种SQL语句\x0d\x0atry{\x0d\x0a ResultSet rs=sql.executeQuery(\x0d\x0a "select * from student")\x0d\x0a }\x0d\x0acatch(SQLException e)\x0d\x0a {\x0d\x0a e.printStackTrace()\x0d\x0a}\x0d\x0a第五步\x0d\x0a获取查询结果\x0d\x0a ResultSet rs=sql.executeQuery(\x0d\x0a "select * from student")\x0d\x0a while(rs.next())\x0d\x0a {\x0d\x0a rs.getString(2)或者是rs.getString("name")\x0d\x0a rs.getInt(3)或者是rs.getInt("age")\x0d\x0a }\x0d\x0a注意\x0d\x0a只有select语句才会有结果集返回;\x0d\x0aResultSet对象一次只能看到一个数据行\x0d\x0a使用next()方法走到下一数据行\x0d\x0a获得一行数据后,ResultSet对象可以使用getXxx()方法获得字段值,将位置索引或字段名传递给get第六步\x0d\x0a关闭创建的各个对象(后打开的先关)\x0d\x0a rs.close()\x0d\x0asql.close()\x0d\x0acon.close()Xxx方法()即可。

1、jsp是java服务端动态网页技术,主要应用于网页构建,理论上讲不应该在页面中直接连数据库。合理的做法是先构建一个java后端,然后在JAVA后端中通过jdbc连接sqlserver。

2、如果一定要在jsp页面中连数据库也是可以的。jsp中有专门的sql标签可以连接数据库进行 *** 作,这是jstl的内容,需要导入相应的数据库驱动包。

3、jsp的内容相对来说技术难度都不算特别高,学习起来还是比较容易的。

4、希望对你有帮助。祝你学有所得。

数据库连接类:

package cn.hpu.bbs.util

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 {

 

 // 定义MySQL的数据库驱动程序

 public static final String DBDRIVER = "com.mysql.jdbc.Driver" 

 //定义mysql的数据库连接地址:

 public static final String DBDURL = "jdbc:mysql://localhost/bbs2014" 

 //mysql数据库的连接用户名

 public static final String DBUSER = "root" 

 //mysql数据库的连接密码

 public static final String DBPASS = "1234" 

 

 public static Connection createConn(){

  Connection conn =null

  try {

   Class.forName(DBDRIVER)

   conn=DriverManager.getConnection(DBDURL,DBUSER,DBPASS)

  } catch (ClassNotFoundException e) {

   e.printStackTrace()

  } catch (SQLException e) {

   e.printStackTrace()

  }

  

  return conn

 }

 

 public static PreparedStatement prepare(Connection conn,String sql){

  PreparedStatement ps=null

  try {

   ps=conn.prepareStatement(sql)

  } catch (SQLException e) {

   // TODO Auto-generated catch block

   e.printStackTrace()

  }

  return ps

 }

 

 public static void close(Connection conn){

  if(conn==null) return

  try {

   conn.close()

   conn=null

  } catch (SQLException e) {

   // TODO Auto-generated catch block

   e.printStackTrace()

  }

 }

 

 public static void close(Statement stmt){

  if(stmt==null) return

  try {

   stmt.close()

   stmt=null

  } catch (SQLException e) {

   // TODO Auto-generated catch block

   e.printStackTrace()

  }

 }

 

 public static void close(ResultSet rs){

  if(rs==null) return

  try {

   rs.close()

   rs=null

  } catch (SQLException e) {

   // TODO Auto-generated catch block

   e.printStackTrace()

  }

 }

}

Category的一个JavaBean:

package cn.hpu.bbs.model

public class Category {

private int id

private String name

private String description

public int getId() {

return id

}

public void setId(int id) {

this.id = id

}

public String getName() {

return name

}

public void setName(String name) {

this.name = name

}

public String getDescription() {

return description

}

public void setDescription(String description) {

this.description = description

}

}

对数据库和Category的 *** 作类://说白了就是增删查修

<pre name="code" class="java">package cn.hpu.bbs.service

import java.sql.Connection

import java.sql.PreparedStatement

import java.sql.ResultSet

import java.sql.SQLException

import java.util.ArrayList

import java.util.List

import cn.hpu.bbs.model.Category

import cn.hpu.bbs.util.DB

public class CategoryService {

public void add(Category c){

Connection conn=DB.createConn()

String sql="insert into category (name,description) values (?,?)"

PreparedStatement ps=DB.prepare(conn, sql)

try {

ps.setString(1, c.getName())

ps.setString(2, c.getDescription())

ps.executeUpdate()

} catch (SQLException e) {

e.printStackTrace()

}

DB.close(ps)

DB.close(conn)

}

public List<Category> list(){

Connection conn=DB.createConn()

String sql="select * from category"

PreparedStatement ps=DB.prepare(conn, sql)

List<Category> categories=new ArrayList<Category>()

try {

ResultSet rs=ps.executeQuery()

Category c=null

while(rs.next()){

c=new Category()

c.setId(rs.getInt("id"))

c.setName(rs.getString("name"))

c.setDescription(rs.getString("description"))

categories.add(c)

}

} catch (SQLException e) {

e.printStackTrace()

}

DB.close(ps)

DB.close(conn)

return categories

}

public void delete(Category c){

deleteById(c.getId())

}

public void deleteById(int id){

Connection conn=DB.createConn()

String sql="delete from category where id=?"

PreparedStatement ps=DB.prepare(conn, sql)

try {

ps.setInt(1, id)

ps.executeUpdate()

} catch (SQLException e) {

e.printStackTrace()

}

DB.close(ps)

DB.close(conn)

}

public void update(Category c){

Connection conn=DB.createConn()

String sql="update category set name = ? , description = ? where id = ?"

PreparedStatement ps=DB.prepare(conn, sql)

try {

ps.setString(1, c.getName())

ps.setString(2, c.getDescription())

ps.setInt(3, c.getId())

ps.executeUpdate()

} catch (SQLException e) {

e.printStackTrace()

}

DB.close(ps)

DB.close(conn)

}

public Category loadById(int id){

Connection conn=DB.createConn()

String sql="select * from category where id=?"

PreparedStatement ps=DB.prepare(conn, sql)

Category c=null

try {

ps.setInt(1, id)

ResultSet rs=ps.executeQuery()

if(rs.next()){

c=new Category()

c.setId(rs.getInt("id"))

c.setName(rs.getString("name"))

c.setDescription(rs.getString("description"))

}

} catch (SQLException e) {

e.printStackTrace()

}

DB.close(ps)

DB.close(conn)

return c

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6692024.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-26
下一篇2023-03-26

发表评论

登录后才能评论

评论列表(0条)

    保存