JDBC joins the database with the formula:pig face extra intuitiveImport Java.sql.connection;import java.sql.driver;import Java.sql.drivermanager;import Java.sql.ResultSet;import Java.sql.sqlexception;import java.sql.Statement; public class Demo03 {public static void main (string[] args) throws ClassNotFoundException, SQLException {///formula: Pig face extra intuitive//register CL Ass.forname ("Com.mysql.jdbc.Driver"); Connection Connection connention = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test3", "root", "MS"); String sql = "SELECT * from student"; Prepare SQL statement//Get Statement object (abbreviation: special) Statement Statement = Connention.createstatement ();//execute ResultSet resultSet = Statement.executequery (SQL); The Get Correlation method column number in the while (Resultset.next ()) {//resultset is starting from 1, remember. System.out.println (Resultset.getint (1) + "," +resultset.getstring (2) + "," +resultset.getint (3) + "," + Resultset.getstring (4));} Close (inverted) resultset.close (); Statement.close (); Connention.close ();}}
- There are three ways to register
- Method One: Class.forName ("Com.mysql.jdbc.Driver");
- Method Two: Driver Driver = new Com.mysql.jdbc.Driver ();
Registered drivermanager.registerdriver (driver); Connection Connection = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test3", "root", "MS");
- Method Three: Drivermanager.registerdriver (new Com.mysql.jdbc.Driver ()); Anonymous Object Registration
Connection Connection = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test3", "root", "MS");
- There are three ways to connect
- Method One: Connection Connection = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test3", "root", "MS");
- Method Two: Connection Connection = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test3?user=root&password =ms ");
- Method Three: String url = "Jdbc:mysql://localhost:3306/test3";
Properties prop = new properties ();p rop.setproperty ("User", "root");p rop.setproperty ("password", "MS"); Connection Connection = drivermanager.getconnection (URL, prop);Interface
- The Java.sql class DriverManager in driver
Common methods are: getconnection (string url, string user, string password) attempt to establish a connection to a given database URL.
- Connection interface: Connection to a specific database (session). Executes the SQL statement in the connection context and returns the result.
A common approach is to have close () immediately release the database and JDBC resources for this Connection object, rather than waiting for them to be freed automatically. Createstatement () Creates a Statement object to send the SQL statement to the database. Preparestatement (String sql) creates a PreparedStatement object to send a parameterized SQL statement to the database.
- Statement interface: An object used to execute a static SQL statement and return the results it produces.
The common approach is that this action occurs when close () immediately releases the database and JDBC resources for this Statement object, rather than waiting for the object to shut down automatically. ExecuteQuery (String sql) executes the given SQL statement, which returns a single ResultSet object executeupdate (String sql) executes the given SQL statement, which may be an INSERT, UPDATE, or DEL A ETE statement, or an SQL statement (such as a SQL DDL statement) that does not return any content.
- PreparedStatement interface
ExtendsStatementAn object that represents a precompiled SQL statement. The SQL statements are precompiled and stored in the PreparedStatement object. You can then use this object to execute the statement efficiently several times.
- ResultSet interface: A data table that represents a database result set, typically generated by executing statements that query the database.
The common approach is that this action occurs when close () immediately releases the database and JDBC resources for this ResultSet object, rather than waiting for the object to shut down automatically. Next () Moves the cursor forward one line from the current position. getInt (int columnindex) Gets the value of the specified column in the current row of this ResultSet object in the form of int in the Java programming language. And the same getbyte,getstring,getlong,getdouble,getboolen,getobject and so on.java.sql.statement class throws SQL Exception
Create a Statement object to send the SQL statement to the database. SQL statements with no parameters are typically executed using the Statement object. If you execute the same SQL statement more than once, it might be more efficient to use the PreparedStatement object. For example: For SELECT queries, statement statement = Connention.createstatement (); ResultSet ResultSet = statement.executequery (sql);
- Preparestatement (String sql)
Create a PreparedStatement object to send the parameterized SQL statement to the database. SQL statements with an in parameter or without an in parameter can be precompiled and stored in the PreparedStatement object. You can then use this object effectively to execute the statement multiple times. Parameters: SQL-may contain one or more '? ' In parameter placeholders for SQL statements the following methods need to be established in order to obtain the correct results for example: methods for creating classes in Java, inserting data in databases public class Accountdaoimpl implements iaccountdao{@ Overridepublic Integer AddAccount (Accountmodel account) {String sql = ' INSERT into student VALUES (NULL,?,?) '; Connection conn = null; PreparedStatement pstmt = Null;integer result = null;try {conn = drivermanager.getconnection ("Jdbc:mysql://localhost : 3306/test8 "," root "," MS ");p stmt = conn.preparestatement (sql);p stmt.setstring (1, Account.getname ()); Pstmt.setdouble (2, Account.getage ()); result = Pstmt.executeupdate ();} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace (); System.out.println ("New failed! ");} Finally {try {dbutil.close (PSTMT, conn);} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} }return result;} Java EE methods for connecting to a database, inserting data into the database please fill in the Servlet class, and you need to create the relevant database, and finally in the Webcontant folderThe HTML file is created and the data is filled in. HTML file as follows//set character encoding several formats request.setcharacterencoding ("Utf-8"); Response.setcharacterencoding ("Utf-8"); Response.setcontenttype ("text/html"); //Connection Database string Name=request.getparameter ("name"); String agestring= request.getparameter ("Age");d ouble age=double.parsedouble (agestring); connection conn=null; PreparedStatement pstmt=null;int result=0;try {conn= drivermanager.getconnection ("jdbc:mysql://localhost:3306/ Test8 "," root "," MS "); String sql= "INSERT into student (name,age) VALUES (?,?)"; Pstmt=conn.preparestatement (SQL);p stmt.setstring (1, name);p stmt.setdouble (2, age); Result=pstmt.executeupdate (); if (result>0) {response.getwriter (). println ("Submit success, thank you!" ");}} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Finally{try {pstmt.close (); Conn.close ();} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace () ;}} java EE Connection Database method, in the database query data//Set character encoding and text format request.setcharacterencoding ("Utf-8"); Response.setcharacterencoding ("UTF-8 "); Response.setcontenttype (" text/html "); //Connection database Connection conn = null; Statement stmt = null; ResultSet rs = null; try {conn = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test1", "root", "MS"); stmt = Conn.createstatement (); rs = Stmt.executequery ("Select Sname,score from ' Program_score '"); PrintWriter pw = Response.getwriter ();p w.println ("<table border= ' 2px solid ' >"), while (Rs.next ()) {Pw.println (" <tr><td> "+rs.getstring (" sname ") +" </td><td> "+rs.getstring (" score ") +" </td></tr > ");} Pw.println ("</table>");} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Finally{try {rs.close (); Stmt.close (); Conn.close ();} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}
- Find select With. executeQuery (String sql);
ResultSet ResultSet = Statement.executequery (StringSQL), add Insert, remove delete, change update with executeupdate () int result=preparedstatement.executeupdate ();ExecuteQueryResultSetExecuteQuery (StringSQL) throwsSQLExceptionExecutes the given SQL statement, which returns a single ResultSet object. Parameters: SQL-The SQL statement to be sent to the database, typically returned as a static SQL SELECT statement: A ResultSet object containing the data generated by the given query; never throw null:SQLException-If a database access error occurs, this method is raised on a closed Statement, or if a given SQL statement generates anything other than a single ResultSet objectexecuteupdateint Executeupdate (StringSQL) throwsSQLExceptionExecutes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement, or an SQL statement (such as a SQL DDL statement) that does not return any content. Parameters: Sql-sql Data manipulation LANGUAGE,DML statements, such as INSERT, UPDATE, or DELETE, or SQL statements that do not return any content, such as DDL statements. Returns: (1) for SQL Data Manipulation Language (DML) statements, the return row count (2) for nothing returns an SQL statement that returns 0 throws:SQLException-If a database access error occurs, this method is raised on the closed Statement, or the given SQL statement generates the ResultSet object
JDBC Connection database method in Java and its application