Idea Operations JDBC Summary

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.