Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stage-0 #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private Connection getConnection() {

@Override
public boolean save(User user) {
return false;
return executeQuery(INSERT_USER_DML_SQL,resultSet -> {return true;}, COMMON_EXCEPTION_HANDLER);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.geektimes.projects.user.service;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.repository.DatabaseUserRepository;
import org.geektimes.projects.user.repository.UserRepository;
import org.geektimes.projects.user.sql.DBConnectionManager;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author zhimingxiao
* @since
*/
public class UserServiceImpl implements UserService{


private UserRepository userRepository = new DatabaseUserRepository(new DBConnectionManager());

@Override
public boolean register(User user) {
return userRepository.save(user);
}


@Override
public boolean deregister(User user) {
return false;
}

@Override
public boolean update(User user) {
return false;
}

@Override
public User queryUserById(Long id) {
return null;
}

@Override
public User queryUserByNameAndPassword(String name, String password) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import org.geektimes.projects.user.domain.User;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
Expand All @@ -16,14 +20,29 @@ public class DBConnectionManager {

private Connection connection;


// JNDI 获取数据源
private static DataSource dataSource;

public void setConnection(Connection connection) {
this.connection = connection;
}

public Connection getConnection() {
return this.connection;
String databaseURL = "jdbc:derby:/db/user-platform;create=true";
Connection connection = null;
try {
connection = DriverManager.getConnection(databaseURL);
this.connection = connection;
return this.connection;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;

}


public void releaseConnection() {
if (this.connection != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
public class HelloWorldController implements PageController {

@GET
@POST
@Path("/world") // /hello/world -> HelloWorldController
public String execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {
return "index.jsp";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.geektimes.projects.user.web.controller;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.repository.DatabaseUserRepository;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.projects.user.service.UserServiceImpl;
import org.geektimes.web.mvc.controller.PageController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Path;
import java.sql.Connection;

/**
* @author zhimingxiao
* @since
*/
@Path("/user")
public class UserController implements PageController {

private static final UserService userService;

static {
userService = new UserServiceImpl();
}



@Override
@Path("/register")
public String execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {
String name = request.getParameter("name");

String phoneNumber = request.getParameter("phoneNumber");
String email = request.getParameter("email");
String password = request.getParameter("password");
User user = new User();
user.setName(name);
user.setPassword(password);
user.setPhoneNumber(phoneNumber);
user.setEmail(email);
userService.register(new User());
return "success.jsp";
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.geektimes.projects.user.web.controller.HelloWorldController
org.geektimes.projects.user.web.controller.UserController
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
url="jdbc:derby:Databases/UserPlatformDB"/>


</Context>
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>

<!-- <resource-ref>-->
<!-- <description>DB Connection</description>-->
<!-- <res-ref-name>jdbc/UserPlatformDB</res-ref-name>-->
<!-- <res-type>javax.sql.DataSource</res-type>-->
<!-- <res-auth>Container</res-auth>-->
<!-- </resource-ref>-->
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/UserPlatformDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


</web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<head>
<jsp:directive.include
file="/WEB-INF/jsp/prelude/include-head-meta.jspf" />
<title>My Home Page</title>
</head>
<body>
<div class="container-lg">
<!-- Content here -->
Hello,World 2021
</div>
</body>