java web 连接数据库

下面只是我个人的一个小项目

java bean

package bean;
public class Image 
{
	private String word;
	private String src;
	public String getWord() {
		return word;
	}
	public void setWord(String word) {
		this.word = word;
	}
	public String getSrc() {
		return src;
	}
	public void setSrc(String src) {
		this.src = src;
	}
}

dao

连接数据库的 引擎,账号密码 因人而异

package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import bean.Image;

public class ImageDao 
{
	public boolean insertImage(String word,String src) throws ClassNotFoundException, SQLException
	{
		Connection conn = null;

			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8", "root", "123456");
			Statement statement = conn.createStatement();
		    int i = 0;
		    String sql = "insert into image (word,src) values(?,?)";
		    PreparedStatement pstmt;

		        pstmt = (PreparedStatement) conn.prepareStatement(sql);
		        pstmt.setString(1, word);
		        pstmt.setString(2, src);
		        i = pstmt.executeUpdate();
		        pstmt.close();
		        conn.close();

		        if(i==0)
		        {
		        	return false;
		        }
		        else
		        {
		        	return true;
		        }
	}
	
	public ArrayList queryAllImages() throws Exception
	{
		Connection conn = null;
		ArrayList images = new ArrayList();
		try {

			// 1 加载驱动程序

			Class.forName("com.mysql.jdbc.Driver");

			// 2 连接数据库

			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");

			// 3 用来执行SQL语句

			Statement statement = conn.createStatement();

			// 要执行的SQL语句

			String sql = "select * from image";

			ResultSet rs = statement.executeQuery(sql);
			
			while(rs.next())
			{
				//实例化VO
				Image image = new Image();
				image.setSrc(rs.getString("src"));
				image.setWord(rs.getString("word"));
				images.add(image);
			}
			
			rs.close();
			statement.close();
			} 
		catch (SQLException e) 
		{
			System.out.println("Sorry,can`t find the Driver!");
			e.printStackTrace();
		} 
		finally
		{
			try
			{
				//关闭连接
				if(conn!=null)
				{
					conn.close();
					conn=null;
				}
			}   catch(Exception ex)
			{
			}
		}
		return images;
	}
}

jar包

两种jar包
连接数据库的jar包导入项目中的lib文件夹中,然后添加到构造路径
mysql和ojdbc两种数据库jar包,导入哪种因人而异

jsp页面中向数据库插入信息

index.jsp

<%@ page language="java" pageEncoding="utf-8" import="java.util.*,java.sql.*"%>
<%@ page import="dao.ImageDao, bean.Image"%>


    
what's up:    image src:   
<% ImageDao imageDao = new ImageDao(); ArrayList images = imageDao.queryAllImages(); %> <% for(int i = 0; i <% } %>
<%=image.getWord() %>

insert.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="dao.ImageDao, bean.Image"%>


  
    <%
         String word= request.getParameter("word");
         String src= request.getParameter("src");
         ImageDao imageDao = new ImageDao();
         imageDao.insertImage(word, src);
         response.sendRedirect("index.jsp");
    %>
  

index.jsp上面是一张要提交信息给insert.jsp的表
下面是读取数据库并显示出来的内容
客户通过index.jsp向insert.jsp提供要插入数据库的信息
insert.jsp实现插入数据库的功能并且跳转回index.jsp首页

你可能感兴趣的:(java)