会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132361个问题
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1321楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1322楼

老师:

    问题1:第一条语句可以搜索到,但是改成 ‘%2005’后一条都搜不到。

select last_name, hire_date from employees where hire_date like '%05';
select last_name, hire_date from employees where hire_date like '%2005';


    问题2:第一条语句可以匹配到一条,把 = 改成 like 后一条都没有,并且第三条语句的 '_7-8月-2005' 表示一个占位符的话应该也能匹配到一吧,但是却一个都匹配不到。

select last_name, hire_date from employees where hire_date = '17-8月-2005';
select last_name, hire_date from employees where hire_date like '17-8月-2005';
select  last_name,  hire_date  from  employees  where  hire_date  like  '_7-8月-2005';


实际运算时的情况如下:

SQL>  select last_name, hire_date from employees where hire_date like '%05';
LAST_NAME                 HIRE_DATE
------------------------- -----------
Kochhar                   2005/9/21
Austin                    2005/6/25
Chen                      2005/9/28
Sciarra                   2005/9/30
Baida                     2005/12/24
Tobias                    2005/7/24
Fripp                     2005/4/10
Vollman                   2005/10/10
Nayer                     2005/7/16
Bissot                    2005/8/20
Atkinson                  2005/10/30
Marlow                    2005/2/16
Stiles                    2005/10/26
Davies                    2005/1/29
Partners                  2005/1/5
Errazuriz                 2005/3/10
Tucker                    2005/1/30
Bernstein                 2005/3/24
Hall                      2005/8/20
Smith                     2005/3/10
LAST_NAME                 HIRE_DATE
------------------------- -----------
Doran                     2005/12/15
Vishney                   2005/11/11
Ozer                      2005/3/11
Hutton                    2005/3/19
Bull                      2005/2/20
Chung                     2005/6/14
Dilly                     2005/8/13
Everett                   2005/3/3
Fay                       2005/8/17
29 rows selected

SQL>  select last_name, hire_date from employees where hire_date like '%2005';
LAST_NAME                 HIRE_DATE
------------------------- -----------

SQL>  select last_name, hire_date from employees where hire_date = '17-8月-2005';
LAST_NAME                 HIRE_DATE
------------------------- -----------
Fay                       2005/8/17

SQL>  select last_name,   hire_date  from  employees  where  hire_date like '17-8月-2005';
LAST_NAME                 HIRE_DATE
------------------------- -----------

SQL>  select last_name, hire_date from employees where hire_date like '%7-8月-2005';
LAST_NAME                 HIRE_DATE
------------------------- -----------





JAVA 全系列/第三阶段:数据库编程/SQL 语言 1323楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 1325楼

问题:老师我用c3p0数据库连接池用配置文件实现连接时报错了,你看是我这个xml文件建的有问题吗,还是c3p0-0.9.1.2.jar这个版本还需要依赖别的jar包,下面是报的错,最下面是配置文件图片

六月 19, 2020 10:37:26 上午 com.mchange.v2.log.MLog <clinit>

信息: MLog clients using java 1.4+ standard logging.

六月 19, 2020 10:37:27 上午 com.mchange.v2.c3p0.C3P0Registry banner

信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]

六月 19, 2020 10:37:27 上午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager

信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 5, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> hellc3p0, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge9eiaa1k63i3np8f2kz|e73f9ac, idleConnectionTestPeriod -> 0, initialPoolSize -> 10, jdbcUrl -> jdbc.mysql://localhost:3306/test, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 100, maxStatements -> 0, maxStatementsPerConnection -> 2, minPoolSize -> 10, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]

六月 19, 2020 10:37:47 上午 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run

警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1f407bb -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!

六月 19, 2020 10:37:47 上午 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run

警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1f407bb -- APPARENT DEADLOCK!!! Complete Status: 

Managed Threads: 3

Active Threads: 3

Active Tasks: 

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@4e0ee3ce (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@6fe6fc6b (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@7f0acc23 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)

Pending Tasks: 

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@17088d3d

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@2e211c81

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@767ca88a

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@5fbe09b2

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@c0c061d

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@2a6fc609

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@39440336

Pool thread stack traces:

Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main]

java.lang.Thread.sleep(Native Method)

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)

com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main]

java.lang.Thread.sleep(Native Method)

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)

com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main]

java.lang.Thread.sleep(Native Method)

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)

com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)



六月 19, 2020 10:37:56 上午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run

警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@4e0ee3ce -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 

java.sql.SQLException: No suitable driver

at java.sql.DriverManager.getDriver(DriverManager.java:315)

at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:223)

at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)

at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)

at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)

at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)

at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)


六月 19, 2020 10:37:56 上午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run

警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@7f0acc23 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 

java.sql.SQLException: No suitable driver

at java.sql.DriverManager.getDriver(DriverManager.java:315)

at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:223)

at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)

at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)

at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)

at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)

at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)


六月 19, 2020 10:37:56 上午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run

警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@6fe6fc6b -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 

java.sql.SQLException: No suitable driver

at java.sql.DriverManager.getDriver(DriverManager.java:315)

at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:223)

at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)

at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)

at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)

at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)

at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)



java.sql.SQLException: Connections could not be acquired from the underlying database!


at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)

at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)

at com.bjsxt7.connection.C3P0Test.testGetConnection1(C3P0Test.java:31)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)

at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)

at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.

at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)

at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)

at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)

... 24 more



Process finished with exit code -1

blob.png

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1326楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 1327楼

老师好:

          示例三,视频里老师测试的两种结果是一样的吧,没看出有什么区别,

第一种:

SQL> select last_name,job_id from employees where job_id like 'SA_%';
LAST_NAME                 JOB_ID
------------------------- ----------
Abel                      SA_REP
Ande                      SA_REP
Banda                     SA_REP
Bates                     SA_REP
Bernstein                 SA_REP
Bloom                     SA_REP
Cambrault                 SA_MAN
Cambrault                 SA_REP
Doran                     SA_REP
Errazuriz                 SA_MAN
Fox                       SA_REP
Grant                     SA_REP
Greene                    SA_REP
Hall                      SA_REP
Hutton                    SA_REP
Johnson                   SA_REP
King                      SA_REP
Kumar                     SA_REP
Lee                       SA_REP
Livingston                SA_REP
LAST_NAME                 JOB_ID
------------------------- ----------
Marvins                   SA_REP
McEwen                    SA_REP
Olsen                     SA_REP
Ozer                      SA_REP
Partners                  SA_MAN
Russell                   SA_MAN
Sewall                    SA_REP
Smith                     SA_REP
Smith                     SA_REP
Sully                     SA_REP
Taylor                    SA_REP
Tucker                    SA_REP
Tuvault                   SA_REP
Vishney                   SA_REP
Zlotkey                   SA_MAN
35 rows selected

第二种:

SQL> select last_name,job_id from employees where job_id like 'SA\_%' escape '\';
LAST_NAME                 JOB_ID
------------------------- ----------
Abel                      SA_REP
Ande                      SA_REP
Banda                     SA_REP
Bates                     SA_REP
Bernstein                 SA_REP
Bloom                     SA_REP
Cambrault                 SA_MAN
Cambrault                 SA_REP
Doran                     SA_REP
Errazuriz                 SA_MAN
Fox                       SA_REP
Grant                     SA_REP
Greene                    SA_REP
Hall                      SA_REP
Hutton                    SA_REP
Johnson                   SA_REP
King                      SA_REP
Kumar                     SA_REP
Lee                       SA_REP
Livingston                SA_REP
LAST_NAME                 JOB_ID
------------------------- ----------
Marvins                   SA_REP
McEwen                    SA_REP
Olsen                     SA_REP
Ozer                      SA_REP
Partners                  SA_MAN
Russell                   SA_MAN
Sewall                    SA_REP
Smith                     SA_REP
Smith                     SA_REP
Sully                     SA_REP
Taylor                    SA_REP
Tucker                    SA_REP
Tuvault                   SA_REP
Vishney                   SA_REP
Zlotkey                   SA_MAN
35 rows selected


JAVA 全系列/第三阶段:数据库编程/SQL 语言 1331楼

封装jdbc

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class jdbcUtil {
    //获取connection对象
    //关闭connection
    //关闭statement对象
    private static String driver="com.mysql.jdbc.Driver";
    private static String connectionUrl="jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8";
    private static String username="root";
    private  static String psw="123456";

    static {
        //读取propertile
        /*ResourceBundle  bundle=ResourceBundle.getBundle("jdbc");
        driver=bundle.getString("driver");
        connectionUrl=bundle.getString("connectionUrl");
        username=bundle.getString("username");
        psw=bundle.getString("psw");*/
        
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        Connection conn=null;
        try {
            conn= DriverManager.getConnection(connectionUrl,username,psw);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void closeStatement(Statement state){
        if(state!=null){
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeConnection(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

测试

package jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class testJdbc {
    public void insertTable(){
        //驱动注册
        Connection conn=null;
        Statement state=null;
        try {
            conn=jdbcUtil.getConnection();
            String str="insert into usertable values(4,'zhao','123','123@1qq')";
            state=conn.createStatement();
            int flag=state.executeUpdate(str);
            System.out.print(flag);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeConnection(conn);
            jdbcUtil.closeStatement(state);
        }

    }
    public static void main(String[] args) {
        testJdbc test=new testJdbc();
        test.insertTable();

    }
}

不用读取properties,运行如下

image.png

我用读取properties文件

properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.connectionUrl=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.psw=123456
package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class jdbcUtil {
    //获取connection对象
    //关闭connection
    //关闭statement对象
    private static String driver";
    private static String connectionUrl;
    private  static String psw;

    static {
        //读取propertile
        ResourceBundle  bundle=ResourceBundle.getBundle("jdbc");
        driver=bundle.getString("driver");
        connectionUrl=bundle.getString("connectionUrl");
        username=bundle.getString("username");
        psw=bundle.getString("psw");
        
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        Connection conn=null;
        try {
            conn= DriverManager.getConnection(connectionUrl,username,psw);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void closeStatement(Statement state){
        if(state!=null){
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeConnection(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

测试类代码没变就改了插入的数据,结果运行如下

image.png

我上网查了原因,没有找到加载该类,初始化没有成功,有些博客说在properties文件中加上jdbc.我加了还是不对

image.png

这是为啥

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1332楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 1333楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 1334楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637