首页 新闻 会员 周边

java数据库测试连接,ct打印出来总是为空?

0
[待解决问题]


import java.sql.*;


public class jdbc {
private Connection ct=null;

public Connection getConn(){
try{

//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//获得连接
Connection ct=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:his", "ZLHIS","NTU");
System.out.println("连接数据库成功");
}catch(Exception e){
//一定要写上·····,不然是这出错什么都不输出
e.printStackTrace();
System.out.println("数据库没连接上");
}
System.out.println(ct);
return ct;
}

 

测试文件

import java.sql.Connection;
import java.sql.Statement;


public class text {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Statement sm=null;
Connection ct=null;
ct=new jdbc().getConn();
if(ct==null){
System.out.print("连接失败");
}else{
System.out.print("连接成功");}


}
}

打印值总是为空,因此显示连接失败,这是怎么回事,求大神指教,我用的数据库是oracle

刘小成的主页 刘小成 | 菜鸟二级 | 园豆:206
提问于:2016-11-14 10:27
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册