JDBC and common data access issues

Source: Internet
Author: User

To connect to a database step:

Registered driver:Class. Forname("Com.mysql.jdbc.Driver");

Set database address, user name, password:String url = "jdbc:mysql://localhost:3306/database";

Create connection:Connection conn = DriverManager. getconnection(URL, username, password);

Create SQL statement string:String sql = "INSERT into Tb_books (Name,price,bookcount,author) VALUES (?,?,?,?)" ;

Using PreparedStatement to process SQL statements:preparedstatement PS = conn.preparestatement (sql);

Set default data:ps.setstring (1, variable);

Executes the SQL statement, returning the number of rows affected:int row = Ps.executeupdate ();

Release object:ps.close ();

Close connection:conn.close ();

     out. println (Request.getparameter (" Price")); DoublePrice = Double.parsedouble (Request.getparameter (" Price"));  out. println (price); Try{class.forname ("Com.mysql.jdbc.Driver"); String URL="jdbc:mysql://localhost:3306/test"; String username="Root"; String Password="123456"; Connection Conn=drivermanager.getconnection (URL, username, password); String SQL="INSERT INTO Tb_books (Name,price,bookcount,author) VALUES (?,?,?,?)"; PreparedStatement PS=conn.preparestatement (SQL); Ps.setstring (1, Book.getname ()); Ps.setdouble (2, Price); Ps.setint (3, Book.getbookcount ()); Ps.setstring (4, Book.getauthor ()); introw =ps.executeupdate (); if(Row >0) {             out. Print ("successfully added the"+ row +"Piece of data! ");        } ps.close ();    Conn.close (); } Catch(Exception e) { out. Print ("Add failed! ");    E.printstacktrace (); }

    Try{class.forname ("Com.mysql.jdbc.Driver"); String URL="jdbc:mysql://localhost:3306/test"; String username="Root"; String Password="123456"; Connection Conn=drivermanager.getconnection (URL, username, password); if(Conn! =NULL) {             out. println ("success!!");        Conn.close (); } Else {             out. println ("fail"); }    } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); }

Q: The decimal number is entered, but the rounded integer is displayed after the deposit.

A:float represents up to 7 bits of valid data. Double represents up to 16 bits of valid data. If you change the number of bits of type double to 0, that's fine.

JDBC and common data access issues

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.