代码如下:
package checkpro;
import java.sql.*;
import java.io.*;
import java.util.*;public class checkdemo {
ArrayList am;
Statement sm;
String str;
Connection con;
BufferedReader buff;
ResultSet rs;
public checkdemo() {
buff=new BufferedReader(new InputStreamReader(System.in));
}
//这个方法用于建立与数据库的连接
void condb()
{
//Class.forName("com.microsoft.jdbc.sqlsever.SQLSeverDriver");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:liu","sa","sa");
sm=con.createStatement();
System.out.println("连接成功!");
} catch (ClassNotFoundException ex) {
} catch (SQLException ex) {
} }
void showcheck()
{
char choice;
while(true)
{
System.out.println("");
System.out.println("1.根据产品代码进行搜索");
System.out.println("2.根据数量进行搜索");
System.out.println("3.退出\n\n");
System.out.println("请输入你的选择.........");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try {
choice = (char) br.read();
switch(choice)
{
case '2':
System.out.println("根据数量搜索产品.......");
showqua();
break;
case '1':
System.out.println("根据产品代码搜索产品.......");
showcode();
break;
default:
System.out.println("byebye");
}
}catch (IOException ex) {
}
}
}
//根据产品代码检索的方法
void showcode()
{
System.out.println("请输入你产品代码:");
try {
String code = buff.readLine();
rs=sm.executeQuery("select * from car where carcode like 'code'");
input(rs);
} catch (IOException ex) {
} catch (SQLException ex) {
/** @todo Handle this exception */
} }
//根据数量检索产品的方法
void showqua()
{
System.out.println("请输入数量 :");
try {
String qua=buff.readLine();
rs=sm.executeQuery("select * from car where quatiry>=qua");
input(rs); } catch (IOException ex) {
} catch (SQLException ex) {
/** @todo Handle this exception */
} }
public static void main(String[] args) {
checkdemo checkdemo = new checkdemo();
checkdemo.condb();
checkdemo.showcheck();
}
//这个方法用集合封装结果集
void input(ResultSet rs)
{
try{
carmessage ma = new carmessage();
while (rs.next()) {
ma.setcarcode(rs.getString(1));
ma.setname(rs.getString(2));
ma.setquatiry(rs.getInt(3));
am.add(ma);
}
for (int i = 0; i < am.size(); i++) {
System.out.print("产品代码:");
System.out.println(((carmessage) am.get(i)).getcarcode());
System.out.print("产品名称");
System.out.println(((carmessage) am.get(i)).getname());
System.out.print("数量");
System.out.println(Integer.toString(((carmessage) am.get(i)).getquatiry()));
}
}catch(Exception e)
{}
}
}
//这个类用于存放汽车的信息
class carmessage
{
private String carcode;
private String name;
private int quatiry;
public String getcarcode()
{
return this.carcode;
}
public void setcarcode(String carcode)
{
this.carcode=carcode;
}
public String getname()
{
return this.name;
}
public void setname(String name)
{
this.name=name;
}
public int getquatiry()
{
return this.quatiry;
}
public void setquatiry(int quatiry)
{
this.quatiry=quatiry;
}}
错误:
Exception in thread "main" java.lang.NullPointerException
at checkpro.checkdemo.showcode(checkdemo.java:69)
at checkpro.checkdemo.showcheck(checkdemo.java:54)
at checkpro.checkdemo.main(checkdemo.java:98)
package checkpro;
import java.sql.*;
import java.io.*;
import java.util.*;public class checkdemo {
ArrayList am;
Statement sm;
String str;
Connection con;
BufferedReader buff;
ResultSet rs;
public checkdemo() {
buff=new BufferedReader(new InputStreamReader(System.in));
}
//这个方法用于建立与数据库的连接
void condb()
{
//Class.forName("com.microsoft.jdbc.sqlsever.SQLSeverDriver");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:liu","sa","sa");
sm=con.createStatement();
System.out.println("连接成功!");
} catch (ClassNotFoundException ex) {
} catch (SQLException ex) {
} }
void showcheck()
{
char choice;
while(true)
{
System.out.println("");
System.out.println("1.根据产品代码进行搜索");
System.out.println("2.根据数量进行搜索");
System.out.println("3.退出\n\n");
System.out.println("请输入你的选择.........");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try {
choice = (char) br.read();
switch(choice)
{
case '2':
System.out.println("根据数量搜索产品.......");
showqua();
break;
case '1':
System.out.println("根据产品代码搜索产品.......");
showcode();
break;
default:
System.out.println("byebye");
}
}catch (IOException ex) {
}
}
}
//根据产品代码检索的方法
void showcode()
{
System.out.println("请输入你产品代码:");
try {
String code = buff.readLine();
rs=sm.executeQuery("select * from car where carcode like 'code'");
input(rs);
} catch (IOException ex) {
} catch (SQLException ex) {
/** @todo Handle this exception */
} }
//根据数量检索产品的方法
void showqua()
{
System.out.println("请输入数量 :");
try {
String qua=buff.readLine();
rs=sm.executeQuery("select * from car where quatiry>=qua");
input(rs); } catch (IOException ex) {
} catch (SQLException ex) {
/** @todo Handle this exception */
} }
public static void main(String[] args) {
checkdemo checkdemo = new checkdemo();
checkdemo.condb();
checkdemo.showcheck();
}
//这个方法用集合封装结果集
void input(ResultSet rs)
{
try{
carmessage ma = new carmessage();
while (rs.next()) {
ma.setcarcode(rs.getString(1));
ma.setname(rs.getString(2));
ma.setquatiry(rs.getInt(3));
am.add(ma);
}
for (int i = 0; i < am.size(); i++) {
System.out.print("产品代码:");
System.out.println(((carmessage) am.get(i)).getcarcode());
System.out.print("产品名称");
System.out.println(((carmessage) am.get(i)).getname());
System.out.print("数量");
System.out.println(Integer.toString(((carmessage) am.get(i)).getquatiry()));
}
}catch(Exception e)
{}
}
}
//这个类用于存放汽车的信息
class carmessage
{
private String carcode;
private String name;
private int quatiry;
public String getcarcode()
{
return this.carcode;
}
public void setcarcode(String carcode)
{
this.carcode=carcode;
}
public String getname()
{
return this.name;
}
public void setname(String name)
{
this.name=name;
}
public int getquatiry()
{
return this.quatiry;
}
public void setquatiry(int quatiry)
{
this.quatiry=quatiry;
}}
错误:
Exception in thread "main" java.lang.NullPointerException
at checkpro.checkdemo.showcode(checkdemo.java:69)
at checkpro.checkdemo.showcheck(checkdemo.java:54)
at checkpro.checkdemo.main(checkdemo.java:98)
没能取得链接,在这里出了错,但是你没有打印出错误
buff=new BufferedReader(new InputStreamReader(System.in));
}
初始化对象有问题,一般不在构造方法里面建立流。一般只在此方法里面生成对象的属性。至于对象的行为最好在其他方法里去处理。你改哈就行了!