[Java] 获取sybase数据库连接 →→→→→进入此内容的聊天室

来自 , 2020-03-19, 写在 Java, 查看 127 次.
URL http://www.code666.cn/view/013a006f
  1. package com.csair.CSMonitor.system.user.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. /**
  10.  * @author yinqingsheng
  11.  *
  12.  * Create on 2012-12-31 上午9:15:27
  13.  *
  14.  * function:获取sybase数据库连接
  15.  */
  16.  
  17.  
  18. import com.sybase.jdbc3.jdbc.SybDriver;
  19. public class SybaseConnection {
  20.         public static  Connection conn = new SybaseConnection().getConnection();
  21.         public static PreparedStatement pStatement;
  22.         public static ResultSet rSet;
  23.         public  Connection getConnection() {
  24.                 String driverClass = "com.sybase.jdbc3.jdbc.SybDriver";
  25.                 String url = "jdbc:sybase:Tds:xxx.xxx.xxx.xxx:5000/et_dcs?charset=cp936";
  26.                 String username = "username";
  27.                 String password = "password";
  28.  
  29.                 try {
  30.                         SybDriver sybDriver = (SybDriver)Class.forName(driverClass).newInstance();
  31.                         Connection connection = DriverManager.getConnection(url, username,
  32.                                         password);
  33.                          return connection;
  34.                 } catch (Exception e) {
  35.                         System.out.println("sybase数据库连接异常!" + e);
  36.                 }
  37.                 return null;
  38.  
  39.         }
  40.        
  41.         public static void closeConnection(){
  42.                 if (rSet!=null) {
  43.                         try {
  44.                                 rSet.close();
  45.                         } catch (SQLException e) {
  46.                                 // TODO Auto-generated catch block
  47.                                 e.printStackTrace();
  48.                         }
  49.                 }
  50.                 if (pStatement!=null) {
  51.                         try {
  52.                                 pStatement.close();
  53.                         } catch (SQLException e) {
  54.                                 // TODO Auto-generated catch block
  55.                                 e.printStackTrace();
  56.                         }
  57.                 }
  58.                 if (conn!=null) {
  59.                         try {
  60.                                 conn.close();
  61.                         } catch (SQLException e) {
  62.                                 // TODO Auto-generated catch block
  63.                                 e.printStackTrace();
  64.                         }
  65.                 }
  66.         }
  67.        
  68.         /*test succeed
  69.         public static void main(String[] args) throws SQLException{
  70.                 if (new SybaseConnection().getConnection()!=null) {
  71.                         System.out.println("success");
  72.                 }else {
  73.                         System.out.println("failed");
  74.                 }
  75.                 String sql = "select * from csmonitor_user where username=?";
  76.                 PreparedStatement pStatement = conn.prepareStatement(sql);
  77.                 pStatement.setString(1, "111");
  78.                 ResultSet rs = pStatement.executeQuery();
  79.                 while (rs.next()) {
  80.                         System.out.println(rs.getString("username") + "---->" + rs.getString("password"));
  81.                        
  82.                 }
  83.         }*/
  84.  
  85. }
  86.  

回复 "获取sybase数据库连接"

这儿你可以回复上面这条便签

captcha