Created
October 4, 2014 11:48
-
-
Save LogIN-/3c51620516003a153c81 to your computer and use it in GitHub Desktop.
Java MySql insert example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package mysqlinsert.solr; | |
| import java.io.IOException; | |
| import java.sql.Connection; | |
| import java.sql.DriverManager; | |
| import java.sql.PreparedStatement; | |
| import java.sql.Statement; | |
| import java.util.Properties; | |
| import org.apache.solr.common.SolrInputDocument; | |
| import org.apache.solr.request.SolrQueryRequest; | |
| import org.apache.solr.response.SolrQueryResponse; | |
| import org.apache.solr.update.AddUpdateCommand; | |
| import org.apache.solr.update.processor.UpdateRequestProcessor; | |
| import org.apache.solr.update.processor.UpdateRequestProcessorFactory; | |
| public class MySQLInsertProcessorFactory extends UpdateRequestProcessorFactory | |
| { | |
| @Override | |
| public UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) | |
| { | |
| return new MySQLInsertProcessor(next); | |
| } | |
| } | |
| class MySQLInsertProcessor extends UpdateRequestProcessor | |
| { | |
| public ConditionalCopyProcessor( UpdateRequestProcessor next) { | |
| super( next ); | |
| } | |
| @Override | |
| public void processAdd(AddUpdateCommand cmd) throws IOException { | |
| SolrInputDocument doc = cmd.getSolrInputDocument(); | |
| Connection conn = null; | |
| Statement s = null; | |
| PreparedStatement ps = null; | |
| String guid; | |
| String fileName; | |
| Object i = doc.getFieldValue( "id" ); | |
| Object f = doc.getFieldValue( "fileName" ); | |
| guid = i.toString(); | |
| fileName = f.toString(); | |
| Properties props = new Properties(); | |
| props.put("user", "root"); | |
| props.put("password", "iv@n1008"); | |
| try | |
| { | |
| conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/solr_test", props); | |
| s = conn.createStatement(); | |
| //I always see only the current record.. not the full record | |
| ps = conn.prepareStatement ( | |
| "INSERT INTO files (guid, filename) VALUES(?,?)"); | |
| ps.setString (1, guid); | |
| ps.setString (2, fileName); | |
| ps.executeQuery(); | |
| s.close(); | |
| ps.close (); | |
| } catch (Exception e){ | |
| System.err.println ("Cannot connect to database server" +e.getMessage()); | |
| } | |
| // pass it up the chain | |
| super.processAdd(cmd); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment