String mysqldriver = application. getInitParameter ("mysqldriver") ;
String mysqlurl = application.getInitParameter("mysqlurl");
String mysqluser = application.getInitParameter("mysqluser");
String mysqlpwd = application.getInitParameter("mysqlpwd");
Class.forName(mysqldriver);
Connection conn = DriverManager.getConnection(mysqlurl, mysqluser, mysqlpwd);
Statement statement = conn.createStatement();
String insert_sql = "insert into user_info(user_id,purchase_end_date,purchase_original_date,purchase_last_date,sessionkey,is_login) values("+user.getUserId()+",'2014-06-23','2014-06-23','2014-06-23','"+sessionKey+"',"+1+")";
try{
statement.execute(insert_sql);
}catch(Exception e){
out.println(e.getMessage());
e.printStackTrace();
}
statement.close();
conn.close();
其中,driver,url,user,pwd都在web.xml中设置,配置代码如下:
<!-- mysql 数据库连接配置 开发环境 -->
<context-param>
<param-name>mysqldriver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>mysqlurl</param-name>
<param-value>jdbc:mysql://127.0.0.1:3306/test</param-value>
</context-param>
<context-param>
<param-name>mysqluser</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>mysqlpwd</param-name>
<param-value>111</param-value>
</context-param>