代码:
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 public class test { 10 11 public static void main(String[] args) throws Exception { 12 getFiberInfo("","",""); 13 14 } 15 16 public static void getConnection(String databaseName, String IP, String port) { 17 String driver = "gauss.jdbc.ZenithDriver"; 18 String url = "jdbc:zentieh:@" + IP + ":" + port; 19 System.out.println(" url:"+ url); 20 21 try { 22 // 加载数据库驱动 23 Class.forName(driver).newInstance(); 24 }catch(Exception e) { 25 // System.out.println("[ErrorType]"+ AutoTestError.PUB_CONNECT_EXCEPTION.getCode() + ":" + AutoTestError.PUB_CONNECT_EXCEPTION.getMsg() + "-" + AutoTestError.FRAMEWORK_DBCONNECT_FAIL.getCode() + ":" + AutoTestError.FRAMEWORK_DBCONNECT_FAIL.getMsg(),true); 26 System.out.println("##异常信息" + e.getMessage()); 27 e.printStackTrace(); 28 return; 29 } 30 31 try{ 32 Connection conn = DriverManager.getConnection(url,databaseName,"Changeme_123"); 33 System.out.println("##zenith数据库连接成功!"); 34 35 }catch (Exception e) { 36 // TODO: handle exception 37 // System.out.println("[ErrorType]"+ AutoTestError.PUB_CONNECT_EXCEPTION.getCode() + ":" + AutoTestError.PUB_CONNECT_EXCEPTION.getMsg() + "-" + AutoTestError.FRAMEWORK_DBCONNECT_FAIL.getCode() + ":" + AutoTestError.FRAMEWORK_DBCONNECT_FAIL.getMsg(),true); 38 System.out.println("##异常信息" + e.getMessage()); 39 } 40 } 41 42 public static List getFiberInfo(String pmiDBName,String pmiIP,String pmiPort) throws SQLException { 43 // 连接存量数据库 44 getConnection(pmiDBName, pmiIP, pmiPort); 45 // 查表tbl_fiber_info中SRCTP字段的值 46 String tblfiberinfo_SQL = "select FIBERID,FIBERNAME from TBL_FIBER_INFO"; 47 List tblfiberinfo = getconnectionBysql_two(tblfiberinfo_SQL); 48 System.out.println("tblfiberinfo= "+ tblfiberinfo); 49 return tblfiberinfo; 50 } 51 //执行查询语句 52 public static List getconnectionBysql_two(String sql) throws SQLException { 53 54 // public static void SelectTest() { 55 Statement stmt = null; 56 Connection conn = null; 57 ArrayList list = new ArrayList(); 58 try { 59 stmt = conn.createStatement(); 60 //执行SELECT语句。 61 ResultSet rs = stmt.executeQuery(sql); 62 while (rs.next()) { 63 String ss = rs.getString(1); 64 String tt = rs.getString(2); 65 String result = ss + "_" + tt; 66 list.add(result); 67 // 如果仅仅打印 68 System.out.println("id:" + rs.getString(1) + ", c_name:" + rs.getString(2)); 69 } 70 71 stmt.close(); 72 } catch (SQLException e) { 73 if (stmt != null) { 74 try { 75 stmt.close(); 76 } catch (SQLException e1) { 77 e1.printStackTrace(); 78 } 79 } 80 e.printStackTrace(); 81 } 82 return list; 83 } 84 85 }