CRUD Operations for Day17 10.JDBC

Source: Internet
Author: User

Each time the driver is registered, gets the connection, and then executes. Every time write very tired, certainly can draw out some things. Java is like this, the same thing can be extracted into a method. It's OK to tune this method when you use it. To what extent is this method extracted?

Package Cn.itcast.jdbc;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.ResultSet;import Java.sql.sqlexception;import Java.sql.statement;import Org.junit.test;import cn.itcast.utils.jdbcutils;//  The CRUD operations for JDBC public class JdbcDemo6 {@Testpublic void Findbyidtest () {//1. sql String sql = "SELECT * * from user where    Id= 1 ";   Connection con = null;   Statement st = null;      ResultSet rs = null; try {//1. Register drive Class.forName ("Com.mysql.jdbc.Driver");//2. Get connection try {con = drivermanager.getconnection ("Jdbc:mysql:    Day17 "," Root "," ");   3. Get operation SQL Statement Object Statement st = Con.createstatement ();      4. Execute SQL RS = st.executequery (SQL);    5. Traverse the result set while (Rs.next ()) {int id = rs.getint ("id");    String id = rs.getstring ("id"),//Although getString () is used, but Getint () is the appropriate string username = rs.getstring ("username");    String Password = rs.getstring ("password");    String email = rs.getstring ("email"); System.out.println (id+ "" +username+ "" +password+ "  "+email); }} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} catch (ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}  finally{//6. Release resource try {if (rs!=null) {rs.close ();  }} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}  try {if (st!=null) {st.close ();       }} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}       try {if (con!=null) {con.close (); }} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} Add Operation @testpublic void Addtest () {//definition SqlString sql = "INSERT into user values (null, ' Zhang San ', ' 123 ', ' [email protected] ' )";   Connection con = null;   Statement st = null;      ResultSet rs = null; try {//1. Register drive Class.forName ("Com.mysql.jdbc.Driver");//2. Get connection try {con = drivermanager.getconnection ("Jdbc:mysql:    Day17 "," Root "," ");   3. Get operation SQL Statement Object Statement st = Con.createstatement (); 4. Execute SQL int row = st.executeupdate (SQL);     System.out.println (row);     if (row!=0) {System.out.println ("add success"); }} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} catch (ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}  finally{//6. Release resource try {if (rs!=null) {rs.close ();  }} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}  try {if (st!=null) {st.close ();       }} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}       try {if (con!=null) {con.close (); }} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} Update operation @testpublic void Updatetest () {//changes the password of id=3 to 456String password = "456"; String sql = "Update user set password= '" +password+ "' where id=3";//1. get connectionconnection con = null;   Statement st = Null;try {con = Jdbcutils.getconnectin ();//3. Get operation SQL Statement object Statement st = Con.createstatement (); 4. Execute SQL int row = St.executeupdate (SQL);     System.out.println (row);     if (row!=0) {System.out.println ("add success"); }} catch (ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (SQLException e) {//To Do auto-generated catch Blocke.printstacktrace ();}  finally{//Close resource try {if (st!=null) {st.close ();       }} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}       try {if (con!=null) {con.close (); }} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

Package Cn.itcast.utils;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.SQLException; public class Jdbcutils {public       static Connection Getconnectin () throws ClassNotFoundException, sqlexception{          Class.forName ("Com.mysql.jdbc.Driver");//In the development of which statement people have the right to choose, you can not give it to extract//2. Get connection  Connection con = Drivermanager.getconnection ("Jdbc:mysql:///day17", "Root", "" ");    return con;}}

CRUD Operations for Day17 10.JDBC

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.