Finally obtained directly through Connfactory.getconn ().
The advantage of this is that when you change the type of database you want to connect to, you just need to change the contents of the configuration file.
second, explain the GetXXX () method
<span style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span> @Testpublic void Getxxxdemo () throws Exception{statement st = Connfactory.getconn (). createstatement (); String sql = "SELECT * from book"; ResultSet rs = st.executequery (sql), while (Rs.next ()) {Integer id = rs.getint (1);//1 here represents the first column in the data table, the following is the same as String name = Rs.getstring (2);//double price = (Double) rs.getobject (3);//out of exception, because the interior is made of BigDecimal to handle Double Price = Rs.getdouble (3); O bject datetime = Rs.getobject (4);//The date and time as a whole read out System.out.println (id+ "," +name+ "," +price+ "," +datetime "); String strdatetime = DateTime.ToString (); System.out.println (strdatetime); strdatetime = Rs.getdate (4) + "--" +rs.gettime (4);//date and time can be obtained separately System.out.println (" ::: "+strdatetime);} Connfactory.getconn (). Close ();} </span>
Note: For your own active growth column. Delete and then insert a new record, the serial number will not go back, continue to grow forward. That is, the middle will appear empty
Iii. explanation of the three executexxx () methods in statement
1. ExecuteQuery: Can only run SELECT statement
2. Executeupdate: Ability to run INSERT, delete, and UPDATE statements, but cannot run select
3, execute: Delete and change the 4 kinds of (random) statements can be run. This method returns False when running a non-select statement, returns True when the SELECT statement is run, and the St object caches the results of that query. I
The result set can be obtained by resultset rs = St.getresultset ()
<span style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span> @Testpublic void Executexxxdemo () throws Exception{statement St = Connfactory.getconn (). createstatement (); String sql = "SELECT * from book";//string sql = "Insert to book (name,price,pub) VALUES (' Software project ', 22.35, ' 2015-12-05 22 : 12:23 ') ";//string sql =" Update book set price=38.88 where name= ' software project ' ";//string sql =" Delete from book where name= ' Software project '; St.executequery (SQL);//st.executeupdate (SQL); Boolean boo = St.execute (sql); if (boo) {ResultSet rs = St.getresultset ( ); while (Rs.next ()) {System.out.println (Rs.getobject (2));}}} </span>
Iv. Anti-black technology in database query (preprocessing statement)
case, user login (splicing SQL statements by user input----very critical)
<span style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span> @Test//user login public void login () throws exception{ Connection con = connfactory.getconn (); Statement st = Con.createstatement (); Scanner sc = new Scanner (system.in); int id = Sc.nextint (); Sc.nextline (); String name = Sc.nextline (); String sql = "SELECT COUNT (*) from stud where id=" +id+ "and Sname= '" +name+ "'"; SYSTEM.OUT.PRINTLN ("sql:" +sql); ResultSet rs = st.executequery (sql), Rs.next (), int a = Rs.getint (1), if (a<=0) {System.out.println ("login unsuccessful");} ELSE{SYSTEM.OUT.PRINTLN ("Login Successful");} Con.close (); } </span>
the Black method. Input: 1002 (Enter) 1 ' or ' 1 ' = ' 1
Therefore: If you need to use the user input information to splice SQL statements, you can not use statement. Otherwise, the user can break through the protection by constructing a true condition in the WHERE clause .
PreparedStatement should be used to solve the situation!
<span style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span> @Test//user login Black: 1002 (enter) 1 ' or ' 1 ' = ' 1public void login2 ( ) throws Exception{scanner sc = new Scanner (system.in); Connection con = connfactory.getconn (); String sql = "SELECT COUNT (*) from stud where id=? and sname=? "; /where user input is required, use placeholder ('? ') To replace. The placeholder is then assigned a value of PreparedStatement PST = con.preparestatement (SQL), and/or set the parameter int id = sc.nextint (); Sc.nextline ();p st.setint (1, id); The 1----represents the 1th placeholder, string name = Sc.nextline ();p st.setstring (2, name),//parameter 2ResultSet rs = Pst.executequery (); Rs.next (); int a = Rs.getint (1), if (a<=0) {System.out.println ("login unsuccessful");} ELSE{SYSTEM.OUT.PRINTLN ("Login Successful");} Con.close (); } </span>
v. Get their own active growth column (e.g. ID)
<span style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span> @Test//Demo Get yourself active growth columns such as idpublic void Saveauto () throws exception{ Connection con = connfactory.getconn (); String sql = "INSERT into book (name,price,pub) VALUES (' Java EE ', 100.8, ' 2013-06-12 08:30:30 ')"; Statement st = Con.createstatement ();//st.executeupdate (SQL); St.executeupdate (Sql,statement.return_generated_keys ); ResultSet rs = St.getgeneratedkeys ();//Inside encapsulates all the values that you actively generate if (Rs.next ()) {int id = rs.getint (1);// Get 1th self-Active growth column System.out.println ("self-actively growing ID:" +id);} System.out.println ("-----------------");//preprocessing statement sql = "INSERT into book (Name,price) VALUES (?,?)"; PreparedStatement PST = con.preparestatement (Sql,statement.return_generated_keys);p st.setstring (1, "Computer based"); Pst.setdouble (2);p st.executeupdate (); ResultSet rs2 = Pst.getgeneratedkeys ();//contains all the values that are actively generated if (Rs2.next ()) {int id = rs2.getint (1);// Get 1th self-Active growth column System.out.println ("self-actively growing ID:" +id);}} </span>
VI. Presentation Batch statements
public void Batch () throws exception{connection con = Connfactory.getconn (); String sql = "INSERT into book (name,price,pub) VALUES (' Java EE ', 100.8, ' 2015-06-12 08:30:30 ')"; Statement st = Con.createstatement (); for (int i=0;i<10;i++) {st.addbatch (SQL);} sql = "Update book set price=price*1.1 where price>100"; St.addbatch (SQL), int[] A = St.executebatch (); for (int r:a) {Syst Em.out.println (R);//r the number of records affected by each SQL statement}}
Pretreatment
public void Preparedbatch () throws exception{connection con = Connfactory.getconn (); String sql = "INSERT into book (name,price,pub) VALUES (?,?,?)"; PreparedStatement PST = con.preparestatement (SQL), for (int i=0;i<5;i++) {pst.setstring (1, "Java" +i);p st.setdouble ( 2, 55.85+i);p st.setstring (3, "2016-12-10 07:07:08");p St.addbatch (); PST cannot be taken as a sql}//pst.executebatch (); int[] A = Pst.executebatch (); for (int r:a) {System.out.println (R);// R is the number of records affected by each SQL statement}}
Note: preprocessingcan not be taken in the form of SQL, ordinary need
What "Java" does to database operations (including preprocessing in the database)