In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use JavaWeb to display mysql database data, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
EMS- employee Information Management system
Summary of the basic Operation of MySQL Learning
Summary of the basic commands of MySQL Learning
Create ems library show databases; create database ems; use ems; create user table create table user (id int primary key auto_increment, name varchar (50), salary double, age int)
Insert tabular data: insert into user values (1); insert into user values (2); insert into user values (3); insert into user values (4)
Create UserListServlets
Connect to the database using JDBC
Copy the jar package to the lib folder
/ / use JDBC to connect to the mysql database, process the output query in the user table, ResultSet result=null; Connection con=null; try {/ / (1) register the load driver Class.forName ("com.mysql.jdbc.Driver"); / / (2) get the link to the database / / (1). Connect to the url address of mysql String url= "jdbc:mysql://localhost:3306/ems"; / / (2). The user name String username= "root" to connect to mysql; / / (3). Password for connecting to mysql String pwd= "123456"; con=DriverManager.getConnection (url, username, pwd); / / (3) precompiled sql statement System.out.println ("MySQL connected successfully!" + con); / / 3. Precompile the SQL statement String sql= "select * from user"; PreparedStatement prep=con.prepareStatement (sql); / / (4) execute the sql statement result=prep.executeQuery (); / / (5) close con.close ();} catch (Exception e) {e.printStackTrace ();} package EMS Import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse / / Servletpublic class UserListServlets extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException of employee information query, IOException {/ / use JDBC to connect to the mysql database, and process the output query in the user table to ResultSet result=null; Connection con=null Try {/ / (1) register the load driver Class.forName ("com.mysql.jdbc.Driver"); / / (2) get the link to the database / / (1). Connect to the url address of mysql String url= "jdbc:mysql://localhost:3306/ems"; / / (2). The user name String username= "root" to connect to mysql; / / (3). Password for connecting to mysql String pwd= "123456"; con=DriverManager.getConnection (url, username, pwd); / / (3) precompiled sql statement System.out.println ("MySQL connected successfully!" + con); / / 3. Precompile the SQL statement String sql= "select * from user"; PreparedStatement prep=con.prepareStatement (sql); / / (4) execute the sql statement result=prep.executeQuery () } catch (Exception e) {e.printStackTrace () } / / use response to obtain the character output stream PrintWriter, and output the query results to the browser side / / set format encoding response.setContentType ("text/html;charset=utf-8") / / output a table PrintWriter pw=response.getWriter (); pw.println ("); pw.println (" employee Information Table "); pw.println ("); pw.println ("Job ID name, salary Age") to the browser Pw.println ("") Try {while (result.next ()) {pw.println ("" + result.getInt ("id") + "+ result.getString (" name ") +"+ result.getDouble (" salary ") +" + result.getInt ("age") + "") System.out.println (result.getInt ("id") + "-" + result.getString ("name") + "-" + result.getDouble ("salary") + "-" + result.getInt ("age")) } catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} pw.println ("") / / shut down try {con.close ();} catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}} deployment server
Redeploy server referenc
Visit the website: http://localhost:8080/Servlet/lists
The above is all the contents of the article "how to use JavaWeb to display mysql database data". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.