Java Class小學堂
進修時寫點心得,將題目生活化,並記錄下一路往前的步步足跡。
2022年8月27日 星期六
2019年11月18日 星期一
Spring MVC 框架中不使用xml實現攔截器(HandlerInterceptor)登入
當要架設需要使用者先登入才能進行其他操作的網站時,
要建立攔截器(HandlerInterceptor)並註冊進Spring MVC中。
2019年10月28日 星期一
各種讀取的方法
//將傳入的textFile檔案轉成字串
public String fileToString(String textFile, String charEncoding) throws Exception {
String msg = "";
try (
InputStream fis = context.getResourceAsStream(textFile);
InputStreamReader isr = new InputStreamReader(fis, charEncoding);
CharArrayWriter caw = new CharArrayWriter();
) {
char[] c = new char[8192];
int len = 0;
while ((len = isr.read(c)) != -1) {
caw.write(c, 0, len);
}
msg = caw.toString();
}
return msg;
}
2019年10月12日 星期六
2019年10月8日 星期二
SQL server如何在有pk或fk的情況下重設識別種子
一般來說,
工程師建立資料表後會先嘗試新增資料看看。
日漸熟悉SQL之後,一定會頻繁使用到Primary Key及Foreign Key。
測試完資料後想要重設附加識別種子的欄位初始值時,
即使Drop and Create或是Drop都沒辦法成功。
此時可以使用下列語法重設欄位初始值:
Hibernate Web JNDI+DataSource+Listener+Filter
1、更改Servers內的context.xml
<Resource
name="jdbc/Jason" type="javax.sql.DataSource"
auth="Container" username="sa" password="passw0rd"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;databaseName=Jason">
</Resource>
2、修改hibernate.cfg.xml
<session-factory>
<!--
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.password">passw0rd</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Jason</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
-->
<property name="hibernate.connection.datasource">java:comp/env/jdbc/Jason</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
</session-factory>
3、撰寫ServletContextListener.java
package util;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SessionFactoryListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
HibernateUtil.getSessionFactory();
System.out.println("Session Factory Created");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
HibernateUtil.closeSessionFactory();
System.out.println("Session Factory Destroyed");
}
}
4、撰寫OpenSessionViewFilter.java
package util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.hibernate.SessionFactory;
public class OpenSessionViewFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
SessionFactory sessionfactory = HibernateUtil.getSessionFactory();
try {
sessionfactory.getCurrentSession().beginTransaction();
System.out.println("Transaction Begin");
chain.doFilter(request, response);
sessionfactory.getCurrentSession().getTransaction().commit();
System.out.println("Transaction Commit");
}catch(Exception e) {
sessionfactory.getCurrentSession().getTransaction().rollback();
System.out.println("Transaction Rollback");
chain.doFilter(request, response);
}finally {
sessionfactory.getCurrentSession().close();
System.out.println("Session Closed");
}
}
}
5、WEB-INF/web.xml內註冊ServletContextListener
<resource-ref>
<description>HibernateSQLServerJDBCConnection</description>
<res-ref-name>jdbc/Jason</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<listener>
<listener-class>util.SessionFactoryListener</listener-class>
</listener>
<filter>
<filter-name>OpenSessionViewFilter</filter-name>
<filter-class>util.OpenSessionViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2019年9月18日 星期三
MySQL的資料表寫法
Create Table Department(
deptid int primary key not null auto_increment,
deptname nvarchar(50)
)AUTO_INCREMENT=1;
Create Table Employee(
empid int primary key not null auto_increment,
empname nvarchar(50),
salary int,
gender nvarchar(50),
deptid int not null,
FOREIGN KEY (deptid) REFERENCES Department(deptid)
)AUTO_INCREMENT=1;
deptid int primary key not null auto_increment,
deptname nvarchar(50)
)AUTO_INCREMENT=1;
Create Table Employee(
empid int primary key not null auto_increment,
empname nvarchar(50),
salary int,
gender nvarchar(50),
deptid int not null,
FOREIGN KEY (deptid) REFERENCES Department(deptid)
)AUTO_INCREMENT=1;
2019年8月20日 星期二
設定JNDI DataSource
設定JNDI DataSource:
在server.xml的<server>中設定DataSource
<Resource driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;databasename=servdb"
username="sa" password="passw0rd" name="jdbc/servdb"
type="javax.sql.DataSource" poolPreparedStatements="true"
auth="Container" maxWait="5000" maxIdle="5" maxActive="120"
initialSize="5" validationQuery="select 1" />
在context.xml的<Context>中設定<ResourceLink>
<ResourceLink
type="javax.sql.DataSource"
global="jdbc/servdb"
name="jdbc/servdb"/>
2019年7月28日 星期日
用JAVA抓取URL上的.json資料
因為google前幾頁找不太到如何從網站上抓取.json寫入java中,
故將研究出來的程式碼記錄下來,
以下以Impl實作方法方式編寫,用政府資料開放平台中的登革熱疫情資料作為示範
訂閱:
意見 (Atom)
無暇的程式碼(Clean code)金句
The only valid measurement of code quality: WTFs/minute.
-
設定JNDI DataSource: 在server.xml的<server>中設定DataSource <Resource driverClassName="com.microsoft.sqlserver.jdbc....
-
//將傳入的textFile檔案轉成字串 public String fileToString(String textFile, String charEncoding) throws Exception { String msg = "...