JDBCは参照を含む添削・改ざん操作を実現する


データベースデータを構築するMySQLコード
tbl_usercreate table tbl_user(
id int(11) unsigned not null auto_increment,
name varchar(50) not null default '',
password varchar(50) not null default '',
email varchar(50) default '',
primary key (id))
engine = InnoDB
default charset = utf8;

create table tbl_address(
id int(11) unsigned not null auto_increment,
city varchar(20) default null,
country varchar(20) default null,
user_id int(11) unsigned not null,
primary key(id))
engine = innodb
default charset = utf8;

insert into tbl_user(id,name,password,email) values
(1,'xiaoming','123456','[email protected]'),
(2,'xiangzhang','123456','[email protected]');

insert into tbl_address (city,country,user_id) values
('beijing','china',1);

insert into tbl_address (city,country,user_id) values
('tianjin','china',2);

DTOクラス、すなわち、データベース内のデータテーブルに対応する、大量の転送を必要とするリモートコールされたオブジェクトデータに使用されるData Transform Objectクラス.
DTOクラスには通常、メンバー変数およびメンバー変数のget、setメソッドのみが含まれ、DTOクラスにはビジネスロジックは含まれません.
すべてのテーブルには、ビジネスロジックのないプライマリ・キーidが含まれており、idのエンティティ・クラスは、ビジネス・クラスのプライマリ・キー情報のカプセル化を実現します.
package EntityPack;

public class IdEntity {
	//                  
	//                
	private Long id;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
}
package EntityPack;

public class UserEntity extends IdEntity{

	private String name;
	private String password;
	private String email;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	@Override
	public String toString() {
		return "UserEntity [name=" + name + ", password=" + password + ", email=" + email + ", getId()=" + getId()
				+ "]";
	}

}
package EntityPack;

public class AddrEntity extends IdEntity{

	private String city;
	private String country;
	private Long userId;
	
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public Long getUserId() {
		return userId;
	}
	public void setUserId(Long userId) {
		this.userId = userId;
	}
	
	@Override
	public String toString() {
		return "AddrEntity [city=" + city + ", country=" + country + ", userId=" + userId + ", getId()=" + getId()
				+ "]";
	}	
	
}

DAOクラス、すなわちData Access Objectクラスで、データアクセスオブジェクトクラスは一般的にデータベースへのアクセスをカプセル化するために使用され、DAOによってデータベース内のテーブルをDTOクラスに変換することができる.
DAOインタフェースクラス
package DaoPack;

import java.sql.Connection;
import EntityPack.UserEntity;

public interface UserDao {
	
	public void save(Connection connection,UserEntity user)throws Exception;
	public void update(Connection connection,UserEntity user,Long id)throws Exception;
	public void delete(Connection connection,UserEntity user)throws Exception ;

}
DAOインタフェースクラスの実装クラス
package ImplPack;

import java.sql.Connection;
import java.sql.PreparedStatement;

import DaoPack.UserDao;
import EntityPack.UserEntity;

public class UserImpl implements UserDao {

	//       
	@Override
	public void save(Connection connection, UserEntity user) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement ps = connection.prepareCall("INSERT INTO tbl_user"
				+ "(name,password,email)VALUES(?,?,?)");
		ps.setString(1, user.getName());
		ps.setString(2, user.getPassword());
		ps.setString(3, user.getEmail());
		
		ps.execute();
	}
	
	//       
	@Override
	public void update(Connection connection, UserEntity user, Long id) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement ps = connection.prepareStatement("UPDATE tbl_user SET name = ?, "
				+ "password = ?, email = ? WHERE id = ?");
		
		ps.setString(1, user.getName());
		ps.setString(2, user.getPassword());
		ps.setString(3, user.getEmail());
		ps.setLong(4,id);
		
		ps.execute();		
	}

	//       
	@Override
	public void delete(Connection connection, UserEntity user) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement ps = connection.prepareStatement("DELETE FROM tbl_user WHERE id = ?");
		
		ps.setLong(1, user.getId());
		
		ps.execute();
	}

}

参照を含むJDBCアクション
package TestPack;

import java.sql.Connection;

import DaoPack.UserDao;
import EntityPack.UserEntity;
import ImplPack.UserImpl;
import PropertyConfigPack.ConnectionFactory;

public class UserTestCls {
	public static void main(String[] args) {
		Connection connection = null;
		try {
			connection = ConnectionFactory.getInstance().makeConnection();
			connection.setAutoCommit(false);
			
			//    DAO
			UserDao userDao = new UserImpl();
			//       
			UserEntity Tom = new UserEntity();
			Tom.setEmail("[email protected]");
			Tom.setName("Tom");
			Tom.setPassword("123456");
			//       
			userDao.save(connection, Tom);
			//     
			connection.commit();
		} catch (Exception e1) {
			// TODO: handle exception
			try {
				//       ,       
				connection.rollback();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
	}
}

クエリー・コード・セグメント
//        
	Class.forName(DBDRIVER) ;
	conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
	//                   
	//                
	String sql = "SELECT name FROM user WHERE userid = ? AND password = ?";
	pstmt = conn.prepareStatement(sql);  // pstmt    
	pstmt.setString(1,request.getParameter("id"));
	pstmt.setString(2,request.getParameter("pwd"));
	rs = pstmt.executeQuery() ;//        
	if(rs.next()){ //      ,     
		flag = true; //       
		name = rs.getString(1);
	}	
}catch(Exception e){
	System.out.println(e.getMessage());
}	
finally{
	try{
		rs.close();
		pstmt.close();
		conn.close();
	}catch(Exception e){
		System.out.println(e.getMessage());
	}	
}
	// flag                          
	if(flag){ //   
		//                  SCRIPT   
%>
		
			
		

		

プロファイルエラー
Tue Aug 02 17:00:34 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
ソリューション、プロファイルを
driver = com.mysql.jdbc.Driver
dburl = jdbc\:mysql\://localhost\:3306/jsp_db?characterEncoding=utf8&useSSL=false
user = root
password = 1233211234567