Yan's

Information Technology Blog


  • Home

  • Categories

  • Archives

  • Tags

review original JDBC operation for MySQL

Posted on 2018-04-20 | In Java |
  • load driver
  • get connection
  • get statement and execute sql
  • get resultSet and handle data
  • close collections to database

Sample code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class OriginalConnection {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String url = "jdbc:mysql://localhost:3306/employee";
String user = "root";
String password = "alec";
Connection conn = null;
// avoid sql injection
PreparedStatement st = null;
ResultSet rs = null;
try {
// load driver for MySQL
// not recommended because it depends on database type
// DriverManager.registerDriver(new com.mysql.jdbc.Driver());
// below coding is strongly recommended, it depends on a string, not one
// database, good for extension and maintenance
// reflection, like framework works
Class.forName("com.mysql.jdbc.Driver");
// ----------------------------------------------------------------
// get connection
// import jdbc API for conn, not a specified database API, it is good for
// maintenace and switching database
// interface-oriented programming
conn = DriverManager.getConnection(url, user, password);
// ----------------------------------------------------------------
// get statement
String sql = "select id, name, password, birthday, email from user where name=?";
// pre-compile sql statement, can speed up execution and relieve database server
st = conn.prepareStatement(sql);
// 1 indicates the first "?"(placeholder)
// setString will check the validity of input, avoid sql injection
st.setString(1, "zhang");
// execute sql and get resultset
// note: do not set sql as a para, just run
// * rs.st.executeQuery(sql) *
rs = st.executeQuery();
// handle data
while (rs.next()) {
System.out.println(rs.getObject("id"));
System.out.println(rs.getObject("name"));
System.out.println(rs.getObject("password"));
System.out.println(rs.getObject("birthday"));
System.out.println(rs.getObject("email"));
}
} finally {
// close, reverse order compared with creating process
// To ensure the closing operation can be executed, these statements should be
// in clause "finally"
// To avoid closing operation occurs exception, add if and try/catch.
if (rs != null) {
try {
rs.close();
} catch (Exception e2) {
// catch exception, JVM go on below code
e2.printStackTrace();
}
rs = null;
}
if (st != null) {
try {
st.close();
} catch (Exception e3) {
// catch exception, JVM go on below code
e3.printStackTrace();
}
st = null;
}
if (conn != null) {
try {
conn.close();
} catch (Exception e4) {
// catch exception, JVM go on below code
e4.printStackTrace();
}
conn = null;
}
}
}
}

Read more »

Hexo Deploy error 1

Posted on 2018-04-20 | In Hexo |

It has been a long time no update on personal website, so when pick it up, and want to post a new file to remote server, there is a error:
could not read Username for 'https://github.com': No error

Uninstall Git and download new version of Git, no problem. Amazing!

Read more »

Common comand for using Hexo

Posted on 2018-04-20 | In Hexo |
create a new article: hexo new “xxx” preview an article: hexo server create static page to “public” directory: hexo generate push new content to GitH ...
Read more »

Hyper-v: cannot open Virtual Switch Manager

Posted on 2017-09-07 | In Linux |

Error Information:
error.PNG


Error Cause:
incomplete installation:
root.PNG

Read more »

Error window pop up when using @ in Eclipse

Posted on 2017-07-30 | In Eclipse |

Error Information:
popup.PNG


Error Describe:
When I use @(annotation) in Eclipse, sometimes there would be a stuck current operation and a window above would pop up.
I am not sure about the reason, but to clean the local repository of Maven and download new .jar, the error does not appear again.

Read more »

Upload file by using SpringMVC CommonsMultipartResolver

Posted on 2017-07-20 | In Java |

Error Information:
HTTP Status 400 – Bad Request
The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).


Error Cause:
dispatcherServlet-servlet.xml

1
2
3
4
5
6
7
<!-- for uploading files -->
<!-- the id should be unique -->
<!-- error occurs -->
<bean id="commonsMultipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
</bean>

Why? Review class DispatcherServlet:

public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver";
Pay more attention to other final variables.


Error Solution:

1
2
3
4
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
</bean>

Read more »

Mapping is missing column attribute for prop

Posted on 2017-07-15 | In Java |

Error Information:
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/github/dao/DepartmentMapper.xml
### The error occurred while processing mapper_resultMap[MyDept]_collection[list]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration.
Cause:org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML.
Cause: java.lang.IllegalStateException: Mapping is missing column attribute for property null


Error Cause:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<resultMap type="com.github.bean.Department" id="MyDept">
<id column="did" property="id" />
<result column="dept_name" property="departmentName" />
<collection property="list" ofType="com.github.bean.Employee">
<id column="eid" property="id" />
<result column="last_name" property="lastName" />
<result column="gender" property="gender" />
<result column="email" property="email" />
<result /> <!-- why is it here, so lonely? No syntax error -->
</collection>
</resultMap>
<select id="getDepByIdPlus" resultMap="MyDept">
select d.id as did,dept_name,e.id as eid,last_name,gender,email
from tbl_employee e,tbl_dept d
where d.id=e.d_id and d.id=#{id}
</select>

Read more »

Add XML catalog in Eclipse for coding prompt

Posted on 2017-07-14 | In Java |

For MyBatis-3.4.4, I need coding prompt from two .dtd files if no Internet:
“http://mybatis.org/dtd/mybatis-3-config.dtd“ for global configuration xml.
“http://mybatis.org/dtd/mybatis-3-mapper.dtd“ for mapper configuration xml.
So set some items like the figure:
set.png

Then close the xml file and then reopen it, done!

Read more »

Maven web project--running error: Unable to compile class for JSP

Posted on 2017-07-12 | In Java |

Error Information:
HTTP Status 500
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 1 in the generated java file
The type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from required .class files


Error Cause:
apache-maven-3.5.0: default running tomcat is Apache Tomcat/6.0.29, but Apache Tomcat/6.0.29 is not available for jdk1.8.0.


Error Solution:
Configure my pom.xml like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
Read more »

Code review and Dao optimization--BaseDao

Posted on 2017-07-07 | In Java |

Reflection: .java to .class, to get .class

  • Class.forName(…)
  • “ClassName”.class
  • “object”.getClass(), this method is in class “Object”

Generic: <>–typeof


Dao optimization:

  • New interface “BaseDao” and its implementing class “BaseDaoImpl”
    1
    2
    3
    4
    5
    6
    7
    public interface BaseDao<T> {
    void add(T t);
    void update(T t);
    void delete(T t);
    T findById(int id);
    List<T> findAll();
    }
Read more »
123
Yan Zhu

Yan Zhu

Simple & Practical

25 posts
6 categories
23 tags
GitHub
© 2017 - 2018 Yan Zhu
Powered by Hexo
Theme - NexT.Mist