Author: Li, Lu teacher 2019-09-23
Ready to work:
1, first install MySQL5.5.30
64: link: https: //pan.baidu.com/s/1H99PH-jRq9OrWBMTA_Xo5g extraction code: 6kmk
32: Link: https: //pan.baidu.com/s/19bBx0s1hC4HWNdYNLB5DeQ extraction code: wdl5
2, mounting navicat for MySQL;
Link: https: //pan.baidu.com/s/1OrM2Q6OSfz7AZojorW4P_A extraction code: 70oy
3, and then create a database - for example, named Stu, create a table stu_info
Programming:
1. Open Eclipse, create Dynamic web project;
2, the link copy jar packets to the MySQL database webcontent-> web-inf-> lib. And right-click the jar package, select buildpath-> add buildpath
jar package Download: link: https: //pan.baidu.com/s/1qlzUjQw3Jk9L2ig0dBlmyg extraction code: qf2i
3, the new JSP file, the instruction page, enter import = "java.sql. *"
4, a JDBC programming steps as follows:
4.1 load the database driver
Class.forName (driverClass)
above dirverClass is the database class driver corresponding class path string, according to different driving different database vendors.
Here MySQL database connection string is: "com.mysql.jdbc.Driver"
4.2 for links to the database via DriverManager
DriverManager.getConnection (String url, Stirng user, String pass)
when using the DriverManager to get the link, you need to pass three parameters: namely, the amount of data url, user name, password.
MySQL database connection parameters are: "jdbc: mysql: // localhost: 3306 / Stu" "root" "root"
Note: The password can be modified when the installation of MySQL for their own password
4.3 Statement object Connection object created, create Connection Statement of the following three methods:
createStatement () to create a basic Statement object.
prepareStatement (String sql): Create a Statement object passed in accordance with pre-compiled sql statement.
prepareCall (String sql): Create a CallableStatement object passed in sql statement
4.4, Statement execute SQL statements, Statement There are three ways to execute SQL statements:
execute: can execute any SQL statement, single more trouble
executeUpdate: can execute DML, DDL statements. Returns the number of SQL DML statements execution of rows affected, execute DDL return 0;
executeQuery: can only execute a query, the query results after the implementation of the return on behalf of the ResultSet object.
4.5, the operating result set for ResultSet
The main move the pointer and obtain a value
next, previous, first, last, beforeFrist, afterLast, absolute movement of the pointer or the like.
get pointer to move getXxx rows, a particular column, the index value. Use column name as a parameter value to obtain good readability, use the index as a parameter to obtain good performance.
4.6, the release of resources
Close result set
Close Statement object
Close Connection object
Core code:
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/Stu", "root", "root");
}catch(Exception e) {
e.printStackTrace();
}
}
public boolean check(User user) throws SQLException {
String userName=user.getUsername();
String pwd=user.getPassword();
String cpwd=null;
boolean flag=false;
try {
PreparedStatement psmt=conn.prepareStatement("select * from stu_info where user=?");
psmt.setString(1, userName);
ResultSet rs=psmt.executeQuery();
while(rs.next()) {
cpwd=rs.getString(2);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
conn.close();
}