请教大神一个相当基础的问题,为什么new线程执行报错,不new线程执行不报错
想知道,为什么代码一会报错,代码二不会报错代码一:
public static void main(String[] args) {
new Thread(new OperateDB()).start();
}
class OperateDB implements Runnable{
@Override
public void run() {
try {
PoolManager.getInstance();
String url="jdbc:microsoft:sqlserver://192.168.1.231:1434;DatabaseName=data_engine";
Connection con = DriverManager.getConnection(url,"sa","scxn");
Connection con2 = DriverManager.getConnection("jdbc:mysql://192.168.1.231:3308/world","root","scxn");
} catch (SQLException e) {
e.printStackTrace();
PoolLogUtils.getInstance().writeLog("未知异常1:"+e.getMessage(),e.getCause().toString(),TestUtils.getDate());
} catch (Exception e) {
e.printStackTrace();
PoolLogUtils.getInstance().writeLog("未知异常2:"+e.getMessage(),e.getCause().toString(),TestUtils.getDate());
}
}
代码二:
public static void main(String[] args) {
try {
PoolManager.getInstance();
String url="jdbc:microsoft:sqlserver://192.168.1.231:1434;DatabaseName=data_engine";
Connection con = DriverManager.getConnection(url,"sa","scxn");
Connection con2 = DriverManager.getConnection("jdbc:mysql://192.168.1.231:3308/world","root","scxn");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
代码一报错为:
--------------------编程问答-------------------- PoolManager.getInstance();是注册驱动等于Class.forName(driverClassName) --------------------编程问答-------------------- 感觉你应该是连接数据库那出的问题吧!
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.io.EOFException
STACKTRACE:
java.io.EOFException
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1913)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2304)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2803)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:812)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1235)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2670)
at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at OperateDB.run(TestMain.java:73)
at java.lang.Thread.run(Unknown Source)
** END NESTED EXCEPTION **
Last packet sent to the server was 8 ms ago.
STACKTRACE:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.io.EOFException
STACKTRACE:
java.io.EOFException
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1913)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2304)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2803)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:812)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1235)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2670)
at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at OperateDB.run(TestMain.java:73)
at java.lang.Thread.run(Unknown Source)
** END NESTED EXCEPTION **
Last packet sent to the server was 8 ms ago.
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2803)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:812)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1235)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2670)
at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at OperateDB.run(TestMain.java:73)
at java.lang.Thread.run(Unknown Source)
** END NESTED EXCEPTION **
Last packet sent to the server was 9 ms ago.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2741)
at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at OperateDB.run(TestMain.java:73)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-0" java.lang.NullPointerException
at OperateDB.run(TestMain.java:88)
at java.lang.Thread.run(Unknown Source)
你使用线程new的话,一个实例会new好几次的!可能我表达的不准确!
你可以先不连接数据库!只在线程里随便输出一个语句!然后启动这个线程,你会发现这个语句输出了多次!
感觉你应该在数据库连接那用下单例吧! --------------------编程问答--------------------
输出了,线程里面就做一件事,输出一行信息,启动线程,只输出了一行. --------------------编程问答-------------------- 好吧,我承认我是新手!不过我把你的代码粘到一个类里怎么提示出错呢? --------------------编程问答-------------------- new Thread(new OperateDB()).start();
执行完后Main线程结束,子线程被强制结束了。
你可以试试这样写:
--------------------编程问答-------------------- 不知道我是否看错,也可能是由于没有看全部代码,可能会给错建议。我根据你给的代码。稍微创建其他代码来验证,
new Thread(new OperateDB()).start();
Thread.sleep(10000); //Main等待10秒
System.out.println("主线程结束");
class Test
{
public static void main(String[] args)
{
new Person().say();
}
class Person {
public void say(){
System.out.println("hello world!");
}
}
}
当编译时,会报错。
class Test
{
public static void main(String[] args)
{
new Person().say();
}
class Person {
public void say(){
System.out.println("hello world!");
}
}
}
所以,我给你的建议是,把这个类调出去。像下面这样。
class Test
{
public static void main(String[] args)
{
new Person().say();
}
}
class Person {
public void say(){
System.out.println("hello world!");
}
} --------------------编程问答-------------------- 执行完后Main线程结束,子线程被强制结束了。 --------------------编程问答--------------------
表示..为嘛我看你的3个都没区别啊?还有哦,oracle,Sqlserver都是可以正常执行的,只是mysql不行 --------------------编程问答-------------------- 你注意看“}”这个,这个我在static void main下多了一个“}”,表明OperatorDB不是内部类。而我看到你上面所传的代码,发现OperatorDB是内部类。这个区别是很大的。所以你把这个OperatorDB类放到外面成一个类,而不是内部类,运行一下。看能否编译成功。这个能否说清楚了???
补充:Java , Java EE