Replacement, the steps to connect to JDBC

Source: Internet
Author: User

The last essay notes the writing of the connection strings for each database, which complements the database connection steps.

1.0 Loading Drive

Use the Class.forName ("DriverName") method to load the corresponding database driver

DriverName: Database Driver class name

           SQL Server: com.microsoft.jdbc.sqlserver.SQLServerDriver

Oracle: Oracle.jdbc.driver.OracleDriver

MySql: com.mysql.jdbc.Driver

          

      the role of Class.forName () is to load the specified class or driver into the JVM for application use.


  1.1 Getting the database Connection object connection  
1.0 after a successful load, the corresponding data instance is registered to the DriverManager class.
Then we pass drivermanager.getconnection ("url", "username", "pwd"); Get the Connection object

URL: Database connection string
Username: Database user name
pwd: Database password
    
The connection to the data through the above steps is done. The next step is to manipulate the database.
1.2 Creating a Statement object Operations database
PS: It is strongly recommended that the PreparedStatement object be manipulated to manipulate the data because PreparedStatement runs the SQL parameter with "?" Pre-compiling SQL for placeholders prevents SQL injection
     
     
    SQL injection:  
        String Sql=select * from user where username= "" and password= ""

Because the stitching character is not checked, it is vulnerable to malicious attacks, such as the following.

SELECT * from user where username= ' Lao li ' or ' 1 ' = ' 1 ' and password= ';

This creates a problem with SQL injection.

    The statement object provides many ways to change database additions and deletions (see code example below)

code example:
    
      
 Packagecom.pf.test;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classJdbctest {PrivateConnection con;  PublicJdbctest ()throwsClassNotFoundException, sqlexception{//1. Load JDBC DriverClass.forName ("Oracle.jdbc.driver.OracleDriver"); String URL= "Jdbc:oracle:thin:@127.0.0.1:1521:test"; String username= "Scott"; String pwd= "Tiger"; //2. Get the Connection objectCon =drivermanager.getconnection (URL,USERNAME,PWD); }        //Add     Public BooleanAdd ()throwssqlexception{String SQL= "INSERT into a values (?,?)"; //Statement Statement = Con.createstatement ();PreparedStatement statement =con.preparestatement (SQL); //assign a value to a placeholderStatement.setint (1, 102); Statement.setstring (2, "Jdbctest"); intExecute =statement.executeupdate (); System.out.println ("Execution Result:" +execute); returnExecute>0; }    //Delete     Public BooleanDelete ()throwssqlexception{String SQL= "Delete a where id=?"; //Statement Statement = Con.createstatement ();PreparedStatement statement =con.preparestatement (SQL); Statement.setint (1, 101); intExecute =statement.executeupdate (); System.out.println ("Execution Result:" +execute); returnExecute>0; }    //Modify     Public BooleanUpdate ()throwssqlexception{String SQL= "Update a set name=?" where id=? "; //Statement Statement = Con.createstatement ();PreparedStatement statement =con.preparestatement (SQL); Statement.setstring (1, "Modify 100"); Statement.setint (2, 100); intExecute =statement.executeupdate (); System.out.println ("Execution Result:" +execute); returnExecute>0; }    //Enquiry     Public voidSearch ()throwssqlexception{String SQL= "SELECT * from a"; //Statement Statement = Con.createstatement ();PreparedStatement statement =con.preparestatement (SQL); ResultSet Set=Statement.executequery ();  while(Set.next ()) {System.out.println ("ID:" +set.getstring ("id") + "\ t name:" +set.getstring ("name"));        System.out.println (); }    }         Public Static voidMain (string[] args)throwsClassNotFoundException, SQLException {jdbctest test=Newjdbctest (); //Test.add (); //Test.delete (); //test.update ();Test.search (); }        }


    
      

Replacement, the steps to connect to JDBC

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.