PackageCom.pkg1;ImportJava.sql.*; Public classJdbcdemo {Connection conn=NULL; //Statement st = null;PreparedStatement st =NULL; PublicJdbcdemo () { This. Init (); } Public Booleaninit () {Try{class.forname ("Sun.jdbc.odbc.JdbcOdbcDriver"); Connection string This. conn = Drivermanager.getconnection ("Jdbc:odbc:test1", "sa", "123456"); Test1 names in ODBC//this.st = This.conn.createStatement (); }Catch(Exception e) {e.printstacktrace (); This. Close (); return false; } return true; }
Public intruncmd (String sql) {intret =-1; Try { This. St = This. conn.preparestatement (SQL); } Catch(SQLException E1) {//TODO auto-generated Catch blockE1.printstacktrace (); } if(St = =NULL){ returnret; } Try{ret=st.executeupdate (SQL); }Catch(Exception e) {e.printstacktrace (); returnret; } returnret; }
String sql = "SELECT * from the person where uid=?" PublicResultSet runQuery (String sql, string uid) {ResultSet rs=NULL; Try { This. St = This. conn.preparestatement (SQL); This. st.setstring (1, UID); Setting Parametersif(St = =NULL){ returnrs; } RS=St.executequery (); } Catch(SQLException E1) {//TODO auto-generated Catch blockE1.printstacktrace (); returnrs; } returnrs; } Public voidClose () {Try{ if(St! =NULL) {st.close (); } if(Conn! =NULL) {conn.close (); } }Catch(Exception ex) {ex.printstacktrace (); } }}
JDBC-ODBC of JDBC Database connection