Java连接sybse数据库

java连接sybase数据库


sybase数据库的驱动程序有两种:

jTDS

JConnect

装载驱动程序

Class.forName( "net.sourceforge.jtds.jdbc.Driver"); //jtds方式
Class.forName( "com.sybase.jdbc3.jdbc.SybDriver"); //jdbc方式

建立连接

Connection conn= DriverManager.getConnection(DB_URL,DB_USERNAME,DB_PASSWORD);

创建对象

Statement st = conn.createStatement();

执行语句并返回结果

st.executeUpdate("SQL statement")
ResultSet rs = st.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
int x = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
}
注意:

By default, only one ResultSet object per Statement object can be open at the same time. 

Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statementobjects. 

All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.


关闭数据库连接

st.close()
conn.close()



package dbconnection;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class SybaseConnection {


    /** Oracle数据库连接驱动*/
    //private final static String DB_DRIVER = "oracle.jdbc.OracleDriver";
    //private final static String DB_DRIVER = "net.sourceforge.jtds.jdbc.Driver";
    private final static String DB_DRIVER = "com.sybase.jdbc3.jdbc.SybDriver";
    
    /** Oracle数据库连接URL*/
    //private final static String DB_URL = "jdbc:oracle:thin:@10.240.70.85:1521:inms";
    //private final static String DB_URL = "jdbc:jtds:sybase://10.230.7.9:4500:DatabaseName=inms";
    private final static String DB_URL = "jdbc:sybase:Tds:10.230.7.9:4500/inms";


    
    /** 数据库用户名*/
    //private final static String DB_USERNAME = "NM31228b1226";
    private final static String DB_USERNAME = "NM31228B";
    /** 数据库密码*/
    //private final static String DB_PASSWORD = "NM31228b1226";
    private final static String DB_PASSWORD = "NM31228B";
    





/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

        /** 声明Connection连接对象*/
        Connection conn = null;
        
        try{
            /** 使用Class.forName()方法自动创建这个驱动程序的实例且自动调用DriverManager来注册它*/
        Class.forName(DB_DRIVER);
       
            /** 通过DriverManager的getConnection()方法获取数据库连接*/
            conn = DriverManager.getConnection(DB_URL,DB_USERNAME,DB_PASSWORD);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        try{
        Statement stmt = conn.createStatement();
        String SelectICONMAP = "SELECT typename,iconname FROM UT_ICONMAP";
        ResultSet rs = stmt.executeQuery(SelectICONMAP);
       
        while (rs.next()) {
        String TypeName = rs.getString("typename");
        String IconName = rs.getString("iconname");
        System.out.print(TypeName + ":"+IconName+"\n");
        }
       
        stmt.close();
        conn.close();
        }catch(Exception ex){
        ex.printStackTrace();
        }
        

}
}

你可能感兴趣的:(java,java,java,sybase连接)