digitalstore.manager.PrimaryKey.getKeyByTableName(PrimaryKey.java:49) 这个类的49行出现的空指针异常。你在这行找找错误吧。把类贴出来也可以啊。
解决方案 »
- 请问JAVA在连接Mysql时候括号错误
- 二级缓存NoCacheRegionFactoryAvailableException
- 配置<url-pattern>不能正常显示servlet
- swing请教
- 孙卫琴的Tomcat与Java Web开发技术详解中的MySQL+JavaBean+JSP的bookstore例子在连接数据库出错,请高手帮解决
- struts2启动时报错
- 求一个在java文件中用getElementsByTagName方法解析XML文件的例子!急!
- 如何截取字符串????
- 怎样获取动态网站的数据?
- 初学问问
- 请教:如何使用Hibernate向Oracle数据库中插入默认值
- Spring配置出错,请高手帮忙,下面是我的代码,,!!!!!
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.DriverManager;
import digitalstore.db.dao.DBConfig;public class PrimaryKey {
//单例模式类,需要先获取单类实例
//线程锁
private static Object initLock=new Object();
//本类的实例作为私有属性,单例模式
private static PrimaryKey primaryKey=null;
//定义一个布尔值
private boolean debug=Constant.DEBUG;
//该方法获取本类的实例
public static PrimaryKey getInstance(){
if(primaryKey == null){
//在括号内放一个对象,在你进行多线程调用的时候虚拟机会自动去检查这个对象是否被锁住了,从而决定该线程是否进入等待状态
synchronized(initLock){
try{
primaryKey=new PrimaryKey();
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
return primaryKey;
} //getKeyByTableName
private static int getKeyByTableName(String tablename){
//设置属性
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//连接数据库
String driver=DBConfig.getInstance().getProperty("driver");
String url=DBConfig.getInstance().getProperty("url");
String username=DBConfig.getInstance().getProperty("username");
String password=DBConfig.getInstance().getProperty("password"); int key=0;
String sql="select newId from primarykey where tablename='"+tablename+"'"; try{
Class.forName(driver);
conn=DriverManager.getConnection(url,username,password);
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
key=rs.getInt("newId");
} //主键加1
int pluskey = key + 1;
//更新保存到数据库
sql="update primarykey set newId=" +pluskey+ " where tableName='"+tablename+"'";
stmt.execute(sql);
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
return key;
} public Integer getUserKey(){
//获取用户名
int n=PrimaryKey.getKeyByTableName("user"); //如果为真
if(debug){
System.out.println("getOrderinfoKey:"+n);
}
return new Integer(n);
} public Integer getOrderinfoKey(){
//获取订单信息
int n=PrimaryKey.getKeyByTableName("Orderinfo"); //如果为真
if(debug){
System.out.println("getOrderinfoKey:"+n);
}
return new Integer(n);
}
public Integer getOrderproductKey(){
//获取产品订单信息
int n=PrimaryKey.getKeyByTableName("Orderproduct"); //如果为真
if(debug){
System.out.println("getOrderproductKey:"+n);
}
return new Integer(n);
} public Integer getProductKey(){
//获取产品信息
int n=PrimaryKey.getKeyByTableName("Product"); //如果为真
if(debug){
System.out.println("getProductKey:"+n);
}
return new Integer(n);
} public static void main(String[] args){
System.out.println(PrimaryKey.getInstance().getUserKey());
System.out.println(PrimaryKey.getInstance().getOrderinfoKey());
System.out.println(PrimaryKey.getInstance().getOrderproductKey());
System.out.println(PrimaryKey.getInstance().getProductKey());
}
}
driver 看看是否正确。还有就是你的表名是一个保留字,最好改掉。(很有可能是这个)
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.DriverManager;
import digitalstore.db.dao.DBConfig; public class PrimaryKey {
//单例模式类,需要先获取单类实例
//线程锁
private static Object initLock=new Object();
//本类的实例作为私有属性,单例模式
private static PrimaryKey primaryKey=null;
//定义一个布尔值
private boolean debug=Constant.DEBUG;
//该方法获取本类的实例
public static PrimaryKey getInstance(){
if(primaryKey == null){
//在括号内放一个对象,在你进行多线程调用的时候虚拟机会自动去检查这个对象是否被锁住了,从而决定该线程是否进入等待状态
synchronized(initLock){
try{
primaryKey=new PrimaryKey();
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
return primaryKey;
} //getKeyByTableName
private static int getKeyByTableName(String tablename){
//设置属性
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//连接数据库
String driver=DBConfig.getInstance().getProperty("driver");
String url=DBConfig.getInstance().getProperty("url");
String username=DBConfig.getInstance().getProperty("username");
String password=DBConfig.getInstance().getProperty("password"); int key=0;
String sql="select newId from primarykey where tablename='"+tablename+"'"; try{
Class.forName(driver);
conn=DriverManager.getConnection(url,username,password);
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
key=rs.getInt("newId");
} //主键加1
int pluskey = key + 1;
//更新保存到数据库
sql="update primarykey set newId=" +pluskey+ " where tableName='"+tablename+"'";
stmt.execute(sql);
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
return key;
} public Integer getUserKey(){
//获取表名
int n=PrimaryKey.getKeyByTableName("user"); //如果为真
if(debug){
System.out.println("getOrderinfoKey:"+n);
}
return n;
}
public static void main(String[] args){
System.out.println(PrimaryKey.getInstance().getUserKey());
}
}
所以就出错了
还又就是,在
//定义一个布尔值
private boolean debug=Constant.DEBUG;
我老是有错误,Constant.DEBUG;不存在。什么原因