Java link database through JDBC
1. Create a table in the database;
Typing MySQL statement, build table: Note the setting of primary key.
Spit the groove, this table is really a lot of fields ...
Then we go back to Java and link the data table with JDBC.
Import java.sql.Connection; Import of these two jar packages is required import Java.sql.DriverManager; public static Connection Connecttodb () {Connection conn = null; String SQL; int result=0; String url = "Jdbc:mysql://192.168.1.134/crawl?useunicode=true&characterencoding=utf-8"; Database URL Required String username = "Zyp"; String password = "zyp123"; Try{class.forname ("Com.mysql.jdbc.Driver");//dynamic load MySQL Driver conn = Drivermanager.getconnection (URL, username, password); Statement stmt = conn.createstatement (); sql = "Delete from crawl"; Stmt.executeupdate (SQL); Return conn; }catch (SQLException e) {System.out.println ("Database connection failed! "); E.printstacktrace (); } catch (Exception e) {e.printstacktrace (); } return conn; }public void Imsertintodb (int questionindex, string question, string questioncategory, int questionglancetimes, Timestamp AskTIME, string questiondetails, int hasacceptedanswer, string acceptedanswer, int acceptedcommenttimes, int ac ceptedgoodtimes, Int acceptedbadtimes, int acceptedresponserrank, int acceptedresponserrate, string acceptedresponserteam, string otheranswer, int othercommenttimes, int othergoodtimes, int otherbadtimes, int otherresponserrank, double scoreofpeoplerank, double Scoreofnumwords, double scoreofgoodtimes, double totalscore, Connection conn) throws sqlexception{try{string sql; sql = "I Nsert into crawl (Questionindex, Question, Questioncategory, Questionglancetimes, AskTIME, Questiondetails, Hasacceptedanswer, Acceptedanswer, Acceptedcommenttimes, Acceptedgoodtimes, Acceptedbadtimes, AcceptedResponserRank , Acceptedresponserrate, Acceptedresponserteam, Otheranswer, Othercommenttimes, Othergoodtimes, OtherBadTimes, Otherresponserrank, Scoreofpeoplerank, Scoreofnumwords, Scoreofgoodtimes, Totalscore) "+" VALUES (?,?,?,?,?,?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; Field placeholder PreparedStatement stmt = conn.preparestatement (sql); Stmt.setobject (1, Questionindex); How many fields do you want to make clear about each one? The Meaning of Stmt.setobject (2, question);Stmt.setobject (3, questioncategory); Stmt.setobject (4, questionglancetimes); Stmt.setobject (5, askTime); Stmt.setobject (6, questiondetails); Stmt.setobject (7, Hasacceptedanswer); Stmt.setobject (8, Acceptedanswer); Stmt.setobject (9, acceptedcommenttimes); Stmt.setobject (Acceptedgoodtimes), Stmt.setobject (one, acceptedbadtimes); Stmt.setobject (12, Acceptedresponserrank); Stmt.setobject (acceptedresponserrate); Stmt.setobject (Acceptedresponserteam); Stmt.setobject (Otheranswer), Stmt.setobject (Othercommenttimes), Stmt.setobject (Othergoodtimes); Stmt.setobject (Otherbadtimes), Stmt.setobject (Otherresponserrank), Stmt.setobject (scoreofnumwords); Stmt.setobject (Scoreofgoodtimes), Stmt.setobject (Scoreofgoodtimes), Stmt.setobject (Totalscore); System.out.println (question); int result; result = Stmt.executeupdate (); }catch (SQLException e) {System.out.println ("MySQL operation Error"); E.printstacktrace (); throw e;}}
Then run Connecttodb in the main function, return the conn of the connection type, and give the INSERTIOTODB parameters conn and the values of the other fields.
When you need to import the database, call the insertiotodb function directly each time.
The effect is as follows:
This will then link the database in Java.
Java link database through JDBC