Skip to content

Commit 3ffffb6

Browse files
committed
把代码开源
0 parents  commit 3ffffb6

25 files changed

+715
-0
lines changed

.classpath

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5+
<classpathentry kind="lib" path="D:/spider/TvQuery/lib/c3p0-0.9.5.2.jar"/>
6+
<classpathentry kind="lib" path="D:/spider/TvQuery/lib/c3p0-oracle-thin-extras-0.9.5.2.jar"/>
7+
<classpathentry kind="lib" path="D:/spider/TvQuery/lib/mchange-commons-java-0.2.11.jar"/>
8+
<classpathentry kind="lib" path="D:/spider/TvQuery/lib/mysql-connector-java-5.1.38-bin.jar"/>
9+
<classpathentry kind="output" path="bin"/>
10+
</classpath>

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>TvQuery</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
eclipse.preferences.version=1
2+
encoding//src/com/jm/bean/ShowItem.java=UTF-8
3+
encoding//src/com/jm/dao/TvQueryUpdate.java=UTF-8
4+
encoding//src/com/jm/db/DbPool.java=UTF-8
5+
encoding//src/com/jm/main/Main.java=UTF-8
6+
encoding//src/com/jm/main/TVQueryThread.java=UTF-8
7+
encoding//src/com/jm/sitehandler/SiteHandler.java=UTF-8
8+
encoding//src/com/jm/util/net/HttpURLConUtil.java=UTF-8

.settings/org.eclipse.jdt.core.prefs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.6
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.6

bin/com/jm/bean/ShowItem.class

1.54 KB
Binary file not shown.

bin/com/jm/dao/TvQueryUpdate.class

2.5 KB
Binary file not shown.

bin/com/jm/db/DbPool.class

1.38 KB
Binary file not shown.

bin/com/jm/db/MyDbPool.class

3.24 KB
Binary file not shown.

bin/com/jm/main/Main.class

2.13 KB
Binary file not shown.

bin/com/jm/main/TVQueryThread.class

3.58 KB
Binary file not shown.
5.35 KB
Binary file not shown.
3.15 KB
Binary file not shown.

c3p0-config.xml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<c3p0-config>
3+
<default-config>
4+
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
5+
<property name="acquireIncrement">3</property>
6+
7+
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
8+
<property name="acquireRetryAttempts">30</property>
9+
10+
<!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
11+
<property name="acquireRetryDelay">1000</property>
12+
13+
<!--连接关闭时默认将所有未提交的操作回滚。Default: false -->
14+
<property name="autoCommitOnClose">false</property>
15+
16+
<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
17+
获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
18+
<property name="breakAfterAcquireFailure">false</property>
19+
20+
<!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒。Default:
21+
0 -->
22+
<property name="checkoutTimeout">100</property>
23+
24+
25+
26+
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
27+
<property name="idleConnectionTestPeriod">60</property>
28+
29+
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
30+
<property name="initialPoolSize">5</property>
31+
32+
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
33+
<property name="maxIdleTime">60</property>
34+
35+
<!--连接池中保留的最大连接数。Default: 15 -->
36+
<property name="maxPoolSize">30</property>
37+
38+
<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
39+
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
40+
<property name="maxStatements">90</property>
41+
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
42+
<property name="maxStatementsPerConnection">3</property>
43+
44+
<!--c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能 通过多线程实现多个操作同时被执行。Default:
45+
3 -->
46+
<property name="numHelperThreads">3</property>
47+
<!--用户修改系统配置参数执行前最多等待300秒。Default: 300 -->
48+
<property name="propertyCycle">300</property>
49+
<!--早期的c3p0版本对JDBC接口采用动态反射代理。在早期版本用途广泛的情况下这个参数 允许用户恢复到动态反射代理以解决不稳定的故障。最新的非反射代理更快并且已经开始
50+
广泛的被使用,所以这个参数未必有用。现在原先的动态反射与新的非反射代理同时受到 支持,但今后可能的版本可能不支持动态反射代理。Default: false -->
51+
<property name="usesTraditionalReflectiveProxies">false</property>
52+
<!-- 填写测试表名字,可以忽略 -->
53+
<property name="automaticTestTable">con_test</property>
54+
<!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒。Default:
55+
0 -->
56+
<property name="checkoutTimeout">30000</property>
57+
<!--每30秒检查所有连接池中的空闲连接。Default: 0 -->
58+
<property name="idleConnectionTestPeriod">30</property>
59+
<!--初始化时获取十个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
60+
<property name="initialPoolSize">10</property>
61+
<!--最大空闲时间,30秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
62+
<property name="maxIdleTime">60</property>
63+
<!--连接池中保留的最大连接数。Default: 15 -->
64+
<property name="maxPoolSize">15</property>
65+
<!--连接池中保留的最小连接数。 -->
66+
<property name="minPoolSize">5</property>
67+
<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
68+
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
69+
<property name="maxStatements">0</property>
70+
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
71+
<property name="maxStatementsPerConnection">0</property>
72+
73+
74+
<!--用户名。Default: null -->
75+
<property name="user">ZHANGBO</property>
76+
<!--密码。Default: null -->
77+
<property name="password">zhangbo123</property>
78+
<!-- 驱动类 -->
79+
<property name="driverClass">com.mysql.jdbc.Driver</property>
80+
<!-- 链接数据库的uri -->
81+
<property name="jdbcUrl">jdbc:mysql://115.28.26.111:3306/tv_query</property>
82+
83+
</default-config>
84+
<!--
85+
<named-config name="dumbTestConfig">
86+
<property name="maxStatements">200</property>
87+
<user-overrides user="poop">
88+
<property name="maxStatements">300</property>
89+
</user-overrides>
90+
</named-config>
91+
-->
92+
</c3p0-config>
93+

lib/c3p0-0.9.5.2.jar

486 KB
Binary file not shown.
2.66 KB
Binary file not shown.

lib/mchange-commons-java-0.2.11.jar

592 KB
Binary file not shown.
961 KB
Binary file not shown.

src/com/jm/bean/ShowItem.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.jm.bean;
2+
3+
public class ShowItem {
4+
private String showTime;
5+
6+
private int weekDay;
7+
private String showDate;
8+
private String showName;
9+
private String channelName;
10+
11+
12+
13+
public ShowItem() {
14+
super();
15+
// TODO Auto-generated constructor stub
16+
}
17+
18+
19+
public ShowItem(String showTime, int weekDay, String showDate,
20+
String showName, String channelName) {
21+
super();
22+
this.showTime = showTime;
23+
this.weekDay = weekDay;
24+
this.showDate = showDate;
25+
this.showName = showName;
26+
this.channelName = channelName;
27+
}
28+
29+
30+
public String getShowTime() {
31+
return showTime;
32+
}
33+
public void setShowTime(String showTime) {
34+
this.showTime = showTime;
35+
}
36+
public int getWeekDay() {
37+
return weekDay;
38+
}
39+
public void setWeekDay(int weekDay) {
40+
this.weekDay = weekDay;
41+
}
42+
public String getShowDate() {
43+
return showDate;
44+
}
45+
public void setShowDate(String showDate) {
46+
this.showDate = showDate;
47+
}
48+
public String getShowName() {
49+
return showName;
50+
}
51+
public void setShowName(String showName) {
52+
this.showName = showName;
53+
}
54+
public String getChannelName() {
55+
return channelName;
56+
}
57+
public void setChannelName(String channelName) {
58+
this.channelName = channelName;
59+
}
60+
61+
62+
}

src/com/jm/dao/TvQueryUpdate.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.jm.dao;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.SQLException;
6+
import java.util.List;
7+
8+
import com.jm.bean.ShowItem;
9+
import com.jm.db.MyDbPool;
10+
11+
public class TvQueryUpdate {
12+
/**
13+
* 将一条节目信息插入数据库
14+
* @param showItem 节目信息 connection调用这个函数的线程的数据库连接
15+
* @return 数据库收到影响的行数
16+
* @throws SQLException
17+
*/
18+
public int insert2show_table(ShowItem showItem,Connection connection){
19+
PreparedStatement preparedStatement = null;
20+
int i = -1;
21+
String sql = "INSERT INTO show_table("
22+
+ "show_time,"
23+
+ "week_day,"
24+
+ "show_date,"
25+
+ "show_name,"
26+
+ "channel_name) VALUES(?,?,?,?,?);";
27+
try {
28+
preparedStatement = connection.prepareStatement(sql);
29+
preparedStatement.setString(1, showItem.getShowTime());
30+
preparedStatement.setInt(2, showItem.getWeekDay());
31+
preparedStatement.setString(3, showItem.getShowDate());
32+
preparedStatement.setString(4, showItem.getShowName());
33+
preparedStatement.setString(5, showItem.getChannelName());
34+
35+
i = preparedStatement.executeUpdate();
36+
} catch (SQLException e) {
37+
// TODO Auto-generated catch block
38+
e.printStackTrace();
39+
} finally {
40+
MyDbPool.attemptClose(preparedStatement);
41+
}
42+
43+
return i;
44+
}
45+
// public boolean batInsert2show_table(List<ShowItem> showItemList,Connection connection){
46+
//
47+
// for(ShowItem showItem:showItemList){
48+
// if(-1 == this.insert2show_table(showItem,connection)){
49+
// return false;
50+
// }
51+
// }
52+
// return true;
53+
// }
54+
public boolean batInsert2show_table(List<ShowItem> showItemList,Connection connection){
55+
PreparedStatement preparedStatement = null;
56+
String sql = "INSERT INTO show_table("
57+
+ "show_time,"
58+
+ "week_day,"
59+
+ "show_date,"
60+
+ "show_name,"
61+
+ "channel_name) VALUES(?,?,?,?,?);";
62+
try {
63+
preparedStatement = connection.prepareStatement(sql);
64+
for(ShowItem showItem:showItemList){
65+
preparedStatement.setString(1, showItem.getShowTime());
66+
preparedStatement.setInt(2, showItem.getWeekDay());
67+
preparedStatement.setString(3, showItem.getShowDate());
68+
preparedStatement.setString(4, showItem.getShowName());
69+
preparedStatement.setString(5, showItem.getChannelName());
70+
preparedStatement.addBatch();
71+
}
72+
preparedStatement.executeBatch();
73+
} catch (SQLException e1) {
74+
// TODO Auto-generated catch block
75+
e1.printStackTrace();
76+
return false;
77+
} finally{
78+
MyDbPool.attemptClose(connection);
79+
}
80+
return true;
81+
}
82+
}

src/com/jm/db/DbPool.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.jm.db;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
6+
import com.mchange.v2.c3p0.ComboPooledDataSource;
7+
/**
8+
* 用枚举模式写的单例模式,确保只有一个线程池
9+
* @author Jacob
10+
*
11+
*/
12+
public enum DbPool{
13+
/**
14+
* 定义一个枚举的元素,它就代表了Singleton的一个实例
15+
**/
16+
INSTANCE;
17+
/**
18+
* 相当于构造函数
19+
*/
20+
DbPool(){
21+
System.setProperty("com.mchange.v2.c3p0.cfg.xml",System.getProperty("com.mchange.v2.c3p0.cfg.xml"));
22+
dataSource=new ComboPooledDataSource();
23+
}
24+
private ComboPooledDataSource dataSource=null;
25+
/**
26+
* 单例可以有自己的操作
27+
**/
28+
public Connection getConnection() throws SQLException{
29+
return dataSource.getConnection();
30+
}
31+
}

src/com/jm/db/MyDbPool.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.jm.db;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.sql.Statement;
8+
9+
10+
import com.mchange.v2.c3p0.ComboPooledDataSource;
11+
12+
public class MyDbPool{
13+
private static ComboPooledDataSource dataSource=null;
14+
static{//类加载的执行一次
15+
//加载配置文件
16+
String path = MyDbPool.class.getProtectionDomain().getCodeSource().getLocation().getPath();
17+
int firstIndex = path.lastIndexOf(System.getProperty("path.separator")) + 1;
18+
int lastIndex = path.lastIndexOf("/") + 1;
19+
path = path.substring(firstIndex, lastIndex)+"c3p0-config.xml";
20+
21+
System.out.println("C3P0配置文件路径:"+path);
22+
System.setProperty("com.mchange.v2.c3p0.cfg.xml",path);
23+
24+
}
25+
//获取连接
26+
public static Connection getConnection() throws SQLException{
27+
if(dataSource==null){//如果为空
28+
dataSource=new ComboPooledDataSource();
29+
}
30+
try{
31+
return dataSource.getConnection();
32+
}catch(SQLException e){
33+
e.printStackTrace();
34+
throw new SQLException();
35+
}
36+
}
37+
//关闭相关东西,释放资源
38+
public static void attemptClose(ResultSet o){
39+
try
40+
{ if (o != null) o.close();}
41+
catch (Exception e)
42+
{ e.printStackTrace();}
43+
}
44+
45+
public static void attemptClose(Statement o){
46+
try
47+
{ if (o != null) o.close();}
48+
catch (Exception e)
49+
{ e.printStackTrace();}
50+
}
51+
52+
public static void attemptClose(Connection o){
53+
try
54+
{ if (o != null) o.close();}
55+
catch (Exception e)
56+
{ e.printStackTrace();}
57+
}
58+
public static void main(String[] a) throws SQLException{
59+
// System.out.println(MyDbPool.class.getResource("c3p0-config.xml").getPath());
60+
61+
Connection con=MyDbPool.getConnection();
62+
String sql="select * from show_table";
63+
PreparedStatement pre=con.prepareStatement(sql);
64+
ResultSet rs=pre.executeQuery();
65+
while(rs.next()){
66+
System.out.println(rs.getString(1));
67+
}
68+
MyDbPool.attemptClose(con);
69+
MyDbPool.attemptClose(pre);
70+
MyDbPool.attemptClose(rs);
71+
}
72+
}
73+

0 commit comments

Comments
 (0)