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