Java, XML and database programming practice (4)

  Protected boolean alreadyin (String tname, String colname, String value) 

  ( 

  Int result; 

  ResultSet rst = null; 

  Try

  / / Implementation of SQL statements 

  String query = "select" colname + + "from" tname + + "where" + colname +"='"+ value +"'"; 

  Statement statement connformax.createStatement = (); 

  Rst = statement.executeQuery (query); 

  If (rst.next ()) 

  ( 

  Statement.close (); 

  Rst.close (); 

  Return true; 

  ) 

  ) 

  Catch (SQLException sqlex) ( 

  Sqlex.printStackTrace (); 

  Return false; 

  ) 

  Return false; 

  ) 

  Protected int getIdfromNumber (String tname, String colname, String value) 

  ( 

  Int result; 

  ResultSet rst = null; 

  Try

  Connection conn = DriverManager.getConnection (destPara.getUrl (), destPara.getUsername (), destPara.getPassword ()); 

  String query = "select id," colname + + "from" tname + + "where" + colname +"='"+ value +"'"; 

  System.out.println (query); 

  Statement statement = conn.createStatement (); 

  Rst = statement.executeQuery (query); 

  If (rst.next ()) 

  ( 

  Return rst.getInt ( "id"); 

  ) 

  ) 

  Catch (SQLException sqlex) ( 

  Sqlex.printStackTrace (); 

  Return 0; 

  ) 

  Return 0; 

  ) 

  / ** 

  * Get a table in the largest id, 

  * / 

  Protected int getMax (String tname) 

  ( 

  Int result; 

  ResultSet rst = null; 

  Try

  / / Implementation of SQL statements 

  String query = "select Max (id) from the" tname +; 

  Statement statement connformax.createStatement = (); 

  Rst = statement.executeQuery (query); 

  If (rst.next ()) 

  ( 

  Return rst.getInt (1) +1; 

  ) 

  ) 

  Catch (SQLException sqlex) ( 

  Sqlex.printStackTrace (); 

  Return 0; 

  ) 

  Return 1; 

  ) 

  / ** 

  * Implementation of a certain sql statement 

  * / 

  Public static void execute (ConnPara connpara, String stmt) throws SQLException 

  ( 

  Connection conn = null; 

  PreparedStatement ps = null; 

  Try

  Conn = DriverManager.getConnection (connpara.getUrl (), connpara.getUsername (), connpara.getPassword ()); 

  System.out.println (stmt); 

  Ps = conn.prepareStatement (stmt); 

  Ps.executeUpdate (); 

  ) Catch (Exception e) ( 

  E.printStackTrace (); 

  System.out.println (e.getMessage ()); 

  Finally () 

  If (ps! = Null) ps.close (); 

  If (conn! = Null) conn.close (); 

  ) 

  ) 

  Public static void main (String argc []) 

  ( 

  DbInput copydb = new DbInput (); 

  Copydb.dbInit (); 

  Copydb.copyproduct (); 

  ) 

  ) 

  Question: 

  1) access the database directly from the jdbc can not read and write, the solution is the first in the ODBC access configuration, and then to operate through ODBC database access. 

  2) implementation find com.microsoft.jdbc.sqlserver.SQLServerDriver category, it is because in running this class, it is necessary to download Microsoft's jdbc package, in this case there are three documents: msbase.jar, mssqlserver.jar, msutil. jar, this document contains three get, there will be no problem. 


Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags:

Releated Articles


0 Comments to “Java, XML and database programming practice (4)”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.