Java Database General Select operation

Source: Internet
Author: User

package py.db.com;import java.awt.list;import java.sql.connection;import  java.sql.drivermanager;import java.sql.preparedstatement;import java.sql.resultset;import  Java.sql.resultsetmetadata;import java.sql.sqlexception;import java.util.arraylist;public class  db_select {public static void main (String[] args)  {testSelect1 ();} Private static void testselect1 ()  {String url =  "Jdbc:mysql://localhost : 3306/test "; string user =  "Root"; string password =  "Uplooking"; String sql1= "Select * from emp"; String sql2= "SELECT&NBSP;*&NBSP;FROM&NBSP;EMP&NBSP;WHERE&NBSP;SAL&NBSP;&GT;&NBSP;?"; String sql3= "SELECT&NBSP;*&NBSP;FROM&NBSP;?"; String sql4= "SELECT&NBSP;*&NBSP;FROM&NBSP;EMP&NBSP;WHERE&NBSP;ENAME&NBSP;=&NBSP;?"; connection conn = null; preparedstatement ps = null; Resultset rs = nUll;try {class.forname ("Com.mysql.jdbc.Driver"); SYSTEM.OUT.PRINTLN ("Database driver loading succeeded"); Conn = drivermanager.getconnection (Url, user, password); SYSTEM.OUT.PRINTLN ("Database connection established successfully");p s = conn.preparestatement (SQL4);p s.setstring (1,  "SMITH"); RS  = ps.executequery ();//------------------------------------------////get column data resultsetmetadata  Rsmd = rs.getmetadata ();//resultsetmetadata is a description of the resultset metadata int colnum =  Rsmd.getcolumncount (); int rownum = 0;//the variable is to get how many lines I've taken java.util.list<object[]> list  = new ArrayList<Object[]> ();while  (Rs.next ())  {rownum++;//system.out.println ( Rs.getstring ("ename"));//Gets the value of a column object[] arrs = new object[colnum];for  (int i  = 1; i <= colnum; i++)  {object obj = rs.getobject (i); Arrs[i-1] = obj;} List.add (ARRS);} System.out.println ("How many rows are recorded: " +rownum);for  (Object[] objects : list)  {for  (object object : objects)  { System.out.print (object +  " ");} System.out.println ();} ------------------------------------------//} catch  (classnotfoundexception e)  { SYSTEM.OUT.PRINTLN ("Database driver loading failed"); E.printstacktrace ();}  catch  (sqlexception e)  {system.out.println ("Database communication Failed"); E.printstacktrace ();}  catch  (exception e)  {e.printstacktrace ();}  finally {try {rs.close ();p s.close (); Conn.close ();}  catch  (exception e2)  {system.out.println ("Object close Failed");}}}


This article is from the "shadow debut" blog, please be sure to keep this source http://woodywoodpecker.blog.51cto.com/4820467/1649754

Java Database General Select operation

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.