当前位置:Gxlcms > 数据库问题 > myEclipse,web项目连接数据库

myEclipse,web项目连接数据库

时间:2021-07-01 10:21:17 帮助过:38人阅读

下面的结构图

技术分享

 

1.在src你创建com.hqyj.wj.model建立model包,model里放数据库的元素,User.java的截图

package com.hqyj.wj.model;
//用户信息表
public class User {
	 private int id;
	 private String name;
	 private String birthday;
	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 getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
}

  

2.在src你创建com.hqyj.wj.dao.inf建立包,这个包是放接口类,这是UserDaoInf的截图

package com.hqyj.wj.dao.inf;
import java.util.List;
import com.hqyj.wj.model.User;
/**
 * 数据访问层的接口定义数据接口的方法
 * 
 */
public interface UserDaoInf {
 //定义一个查询方法
	List<User> search();
	
}

3.在src你创建com.hqyj.wj.dao建立包,这个包连接数据库,及实现上面的接口类

package com.hqyj.wj.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

import com.hqyj.wj.dao.inf.UserDaoInf;
import com.hqyj.wj.model.User;

/**
 * 
 * @author wl 数据访问接口
 */
public class UserDao implements UserDaoInf {
	// 数据访问数据库的连接对象
	protected Connection con = null;
	// 获取数据库链接
	@SuppressWarnings("finally")
	public Connection getCon() {
		try {
			// 加载mysql驱动
			Class.forName("com.mysql.jdbc.Driver");
			//获取数据库链接
			con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/image?characterEncoding=utf8", "root", "root");
			System.out.println("链接成功");
			return con;
		} catch (Exception e) {
			System.out.println("链接失败"+e.getMessage());
			return null;
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace(); 
			}
			return null;
		}

	}

	public List<User> search() {

		return null;
	}

}

  

4.在src你创建com.hqyj.wj.controller建立包,里面放的new->servlet

 

package com.hqyj.wj.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OneServlet extends HttpServlet {

	public OneServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
  //doget对应的是ajax的$.get()方法
  //request是装载请求数据
//response响应数据到前端对象
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
      		//解决中文乱码的问题
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//在服务器端设置允许在其他域名下访问,及响应类型、响应头设置
		System.out.println("这是");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods","POST");
		response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
		  //响应的文本格式
		response.setContentType("text/html");
		//获取响应的输出对象
		PrintWriter out = response.getWriter();
		out.print("nissssss");
		out.print("您好呀");
		out.flush();
		out.close();
	}
	  //doget对应的是ajax的$.post()方法
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

5.要引入数据库包mysql-connector-java-commercial-5.1.25-bin.jar,在项目名字上右键->properties->java Build Path->Libraries->Add External Jars然后选择你下载的包

6.在src下面创建测试类

import com.hqyj.wj.dao.UserDao;

public class Test {

public static void main(String[] args) {
//获取数据库访问类对象
UserDao dao=new UserDao();
dao.getCon();
}

}

  

7.成功显示

技术分享

 

myEclipse,web项目连接数据库

标签:.post   响应   utf8   数据   class   on()   aries   sea   body   

人气教程排行