Learning Java from scratch -- getting started with JDBC database development

Source: Internet
Author: User

Using MYSQL as an example to describe how to operate a database in JSP:

1. Add the driver of the corresponding database, for example, MySQL is the mysql-connector-java-5.1.25-bin.jar

2. Create the mysqltest. jsp page and enter the following code for testing.

<% @ Page contenttype = "text/html; charset = gb2312" %> <% @ page Language = "Java" %> <% @ page import = "com. mySQL. jbdc. * "%> <% @ page import =" Java. SQL. * "%> <% string drivername =" com. mySQL. JDBC. driver "; string username =" root "; string userpwd =" 123456 "; string dbname =" test "; string tablename =" goods "; string url =" JDBC: mysql: // localhost/"+ dbname + "? User = "+ username +" & Password = "+ userpwd; Class. forname ("com. mySQL. JDBC. driver "). newinstance (); Java. SQL. connection conn = drivermanager. getconnection (URL); out. print ("database connection successful! "); Out. print ("<br>"); Java. SQL. statement statement = Conn. createstatement (); string SQL = "select * from" + tablename; Java. SQL. resultset rs1_statement.exe cutequery (SQL); out. print ("Commodity Code"); out. print ("|"); out. print ("Product Name"); out. print ("|"); out. print ("price (RMB)"); out. print ("<br>"); While (RS. next () {out. print (RS. getstring (1) + ""); out. print ("|"); out. print (RS. getstring (2) + ""); out. print ("|"); out. print (RS. getstring (3) + ""); out. print ("<br>");} Rs. close (); statement. close (); Conn. close (); %>

 

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.