Learn idea today operation Jdbc,mysql Package import to Project
1, the Registration database driver
Driver driver=new com.mysql.jdbc.Driver ();
Drivermanager.registerdriver (driver);
String Driver ="Com.mysql.jdbc.Driver";
Class.forname (Driver);Java Base Reflection
2. Get a database connection
String URL ="Jdbc:mysql://localhost:3306/fresh";
Connection Connection = DriverManager.getconnection (URL,"Root","");
3. Get the objects that manipulate the database
Statement Statement = Connection.createstatement ();
String sql = "select * from user " ResultSet ResultSet = statement.executequery (sql) ;
//4. Traverse result set, take out data
Span style= "COLOR: #808080" > while (Resultset.next ()) {
String name = Resultset.getstring ( "name") System. out.print ( "name:" +name) ;
}
Connection.close () ;
Update data
String sql ="UPDATE user set name= ' Little Moe ' where id=1";
int resule = statement.executeupdate (SQL);
System. Out.println (resule)
Statement.close () ;
Connection.close ();
junit unit test
Project = "New test directory, Project structure=> Select the test file click sources so you can create a Java file under the test folder
Idea Operations JDBC Summary