Java Operations Database

Source: Internet
Author: User

Reference: https://www.cnblogs.com/centor/p/6142775.html

MySQL Connection driver: Mysql-connector-java-5.1.27.jar

1  PackageSqldemo;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 ImportJava.sql.ResultSet;6 Importjava.sql.SQLException;7 Importjava.sql.Statement;8 9  Public classMain {Ten  One      Public Static voidMain (string[] args) { A         //declaring connection Objects - Connection con; -         //driver name theString Driver = "Com.mysql.jdbc.Driver"; -         //the URL points to the database name you want to access MyData -String url = "Jdbc:mysql://localhost:3306/sqltestdb"; -         //user name when MySQL is configured +String user = "root"; -         //password for MySQL configuration +String password = "123456"; A         //traversing query result sets at         Try { -             //Load Driver - Class.forName (driver); -             //1.getConnection () method, connect MySQL database!!  -Con =drivermanager.getconnection (Url,user,password); -             if(!con.isclosed ()) inSYSTEM.OUT.PRINTLN ("Succeeded connecting to the database!"); -             //2. Create the statement class object to execute the SQL statement!!  toStatement Statement =con.createstatement (); +             //the SQL statement to execute -String sql = "SELECT * from emp"; the             //3.ResultSet class, used to store the obtained result set!!   ResultSet rs = statement.executequery (sql);  $System.out.println ("-----------------");Panax NotoginsengSystem.out.println ("The execution results are as follows:");  -System.out.println ("-----------------");  theSystem.out.println ("name" + "\ T" + "title");  +System.out.println ("-----------------");  A               theString job =NULL; +String ID =NULL; -              while(Rs.next ()) { $                 //get stuname This column of data $Job = rs.getstring ("Job"); -                 //get stuid This column of data -id = rs.getstring ("ename"); the  -                 //Output ResultsWuyiSystem.out.println (id + "\ T" +job); the             } - rs.close (); Wu con.close ();//close Connection -}Catch(ClassNotFoundException e) { About             //database-driven class exception handling $System.out.println ("Sorry,can ' t find the driver!");  - E.printstacktrace ();  -}Catch(SQLException e) { -             //database connection failure exception handling A E.printstacktrace ();  +}Catch(Exception e) { the             //Todo:handle Exception - e.printstacktrace (); $}finally{ theSYSTEM.OUT.PRINTLN ("Database data successfully obtained!! "); the         } the     } the  -}

String name;  String ID; PreparedStatement Psql; ResultSet Res;//preprocessing adds data, which has two parameters--"? "Psql = Con.preparestatement ("INSERT into EMP (empno,ename,job,hiredate,sal)" + "VALUES (?,?,?,?,?)");p Sql.setint (1, 3212);//Set parameter 1 to create data with ID 3212Psql.setstring (2, "Wang Gang");//Set parameter 2,name to Wang gangPsql.setstring (3, "President"); DateFormat DATEFORMAT2=NewSimpleDateFormat ("Yyyy-mm-dd");D ate myDate2= Dateformat2.parse ("2010-09-13"));p sql.setdate (4,Newjava.sql.Date (Mydate2.gettime ()));p Sql.setfloat (5, (float) 2000.3);           psql.executeupdate ();  // perform updates
Psql.close;

PreparedStatement psql; // preprocess Update (Modify) data, change the Wang Gang's sal to 5000.0psql = con.preparestatement ("update emp set sal =?"). where ename =? " );p sql.setfloat (1, (float) 5000.0);      Psql.setstring (2, "Wang Gang");             Psql.executeupdate ();
Psql.close ();

PreparedStatement psql; // preprocessing Delete data psql = con.preparestatement ("Delete from emp where Sal >?") );p sql.setfloat (1, 4500);p sql.executeupdate (); psql.close (); 

Java Operations Database

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.