1 package testsample001;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8
9 public class check {
10 private Connection con;
11 private String user = "deejuser";
12 // private String user = "sys as sysdba";
13 private String password = "deejuser";
14 private String className = "oracle.jdbc.driver.OracleDriver";
15 // private String url="jdbc:oracle:oci@localhost:1158:orcl";这个url可能无效
16 private String url = "jdbc:oracle:thin:@zs-PC:1521:ytdf";
17
18 public String ConnectOracle(String productno) {
19 try {
20 Class.forName(className);
21 // System.out.println("加载数据库驱动成功!");
22 // System.out.println(productno);
23 String resultInfo = getCon(productno);
24 closed();
25 return resultInfo;
26 } catch (ClassNotFoundException e) {
27 // System.out.println("加载数据库驱动失败!");
28 e.printStackTrace();
29 return "加载数据库驱动失败!";
30 }
31 }
32
33 /** 创建数据库连接 */
34 // public Connection getCon(String productno) {
35 public String getCon(String productno) {
36 try {
37 con = DriverManager.getConnection(url, user, password);
38 // System.out.println("创建数据库连接成功!");
39 Statement stmt = con
40 .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
41 ResultSet.CONCUR_UPDATABLE);
42 String sql = "select * from product t where t.packagsn="
43 + productno;
44 // System.out.print(sql);
45 ResultSet rs = stmt.executeQuery(sql);
46 if (rs.next())
47 return "防伪码存在!";
48 else
49 return "防伪码不存在!";
50 } catch (SQLException e) {
51 // System.out.print(con);
52 // System.out.println("创建数据库连接失败!");
53 con = null;
54 e.printStackTrace();
55 return "数据库连接失败!";
56 }
57 // return con;
58 }
59
60 public void closed() {
61 try {
62 if (con != null) {
63 con.close();
64 }
65 } catch (SQLException e) {
66 System.out.println("关闭con对象失败!");
67 e.printStackTrace();
68 }
69 }
70 }