在JSP中使用数据库

需要用到的工具、软件:数据库(MySQL或者SQL Service) 以及相对应的jdbc链接器(连接器的对应版本不一样会导致连接失败) eclipse软件  Navivat(没有也行)

我用的版本:

 

一、首先连接数据库,在数据库中建立一个名为Book的数据库(名字自己取其他的也行),新建一个名为booklist的表,添加内容

如图:

 记住自己的用户名以及密码,之后要用到

一般MySQL的是root ; sql service 的是sa 。我用的是MySQL 并且密码设置为:123456

二、把jdbc连接器放到WebContent下的WEB-INF下的lib下;要放入相对应的连接器(很重要)

三、建立java类准备实现连接

 完整代码:

package jdbc.example01;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class example02 {
	public static void main(String[] args)throws ClassNotFoundException {
		Connection con=null;
		Statement sql=null;
		ResultSet rs=null;
		try{ //加载MySQL连接器
		Class.forName("com.mysql.cj.jdbc.Driver");
		//sql service的 :Class.forName("com.microsoft.sqlservice.jdbc.SQLServerDriver");
		//连接数据库
		String url="jdbc:mysql://localhost:3306/book?"+"useSSL=false&serverTimezone=CST&characterEncoding=utf-8";
		String username="root";
		String password="123456";
		
		con=DriverManager.getConnection(url,username,password);
		sql=con.createStatement();
		
		sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
		//插入
		String insertSql="insert into bookList values('1547852','C语言程序设计',55,'2017-5-14')";
		int result=sql.executeUpdate(insertSql);
		System.out.println(result);
		if(result>0)
			System.out.println("插入成功");
		//删除
		String deleteSql="delete from bookList where name='高等数学'";
		int result1=sql.executeUpdate(deleteSql);
		System.out.println(result1);
		if(result1>0)
			System.out.println("删除成功");
		//更新
		String updateSql="update bookList set price=48 where name='大学英语'";
		int result2=sql.executeUpdate(updateSql);
		System.out.println(result2);
		if(result2>0)
			System.out.println("更新成功");
		
		String SQL = "select * from booklist";
		rs=sql.executeQuery(SQL);
		while(rs.next()){
			String isbn =rs.getString("ISBN");
			String name =rs.getString("name");
			float price = rs.getFloat("price");
			Date date= rs.getDate("date");
			
			System.out.println(isbn+"--"+name+"--"+price+"--"+date);				
		   }		
	    }	
		
		
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		//关闭与释放
		finally {
			if(rs!=null) {
				try {
					rs.close();
				}catch(SQLException e) {
					e.printStackTrace();
				}
				rs=null;
			}
			if(con!=null) {
				try {
					con.close();
				}catch(SQLException e) {
					e.printStackTrace();
				}
				con=null;
			}
			if(sql!=null) {
				try {
					sql.close();
				}catch(SQLException e) {
					e.printStackTrace();
				}
				sql=null;
			}
		}
		
		
	}

}

连接到使用到结束大概可以分为六部分

1.注册数据库驱动

Class.forName("com.mysql.cj.jdbc.Driver");
		//sql service的 :Class.forName("com.microsoft.sqlservice.jdbc.SQLServerDriver");

2.通过Drviver Manager 获取数据库连接

url地址:如果是本地机子直接用localhost就好,后面的端口号,MySQL是3306,SQL service是1433,这是规定的,端口号后面的就是数据库名:book(用你自己取的替代它)

//连接数据库
		String url="jdbc:mysql://localhost:3306/book?"+"useSSL=false&serverTimezone=CST&characterEncoding=utf-8";
		String username="root";
		String password="123456";

3.通过Connection对象获取statement对象

con=DriverManager.getConnection(url,username,password);
		sql=con.createStatement();

4.使用statement对象执行sql语句  rs=sql.executeQuery(SQL)

查询:

String SQL = "select * from booklist";
		rs=sql.executeQuery(SQL);
		while(rs.next()){
						
		   }		
	    }	

删除:

String deleteSql="delete from bookList where name='高等数学'";
		int result1=sql.executeUpdate(deleteSql);
		System.out.println(result1);
		if(result1>0)
			System.out.println("删除成功");

更新:

String updateSql="update bookList set price=48 where name='大学英语'";
		int result2=sql.executeUpdate(updateSql);
		System.out.println(result2);
		if(result2>0)
			System.out.println("更新成功");

插入:

String insertSql="insert into bookList values('1547852','C语言程序设计',55,'2017-5-14')";
		int result=sql.executeUpdate(insertSql);
		System.out.println(result);
		if(result>0)
			System.out.println("插入成功");

5.操作resultset结果集

String isbn =rs.getString("ISBN");
			String name =rs.getString("name");
			float price = rs.getFloat("price");
			Date date= rs.getDate("date");
			
			System.out.println(isbn+"--"+name+"--"+price+"--"+date);	

6.关闭和释放语句

		finally {
			if(rs!=null) {
				try {
					rs.close();
				}catch(SQLException e) {
					e.printStackTrace();
				}
				rs=null;
			}
			if(con!=null) {
				try {
					con.close();
				}catch(SQLException e) {
					e.printStackTrace();
				}
				con=null;
			}
			if(sql!=null) {
				try {
					sql.close();
				}catch(SQLException e) {
					e.printStackTrace();
				}
				sql=null;
			}
		}
		

  • 19
    点赞
  • 121
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醉游星河

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值