Before the company directly used in the database is used Ibatis, now to learn some JDBC programming, by the way compare with the Ibatis framework of the difference.
One, use the steps:
1, load driver.
Class.forName ("Com.mysql.jdbj.Driver");
2, use DriverManager to get the database connection. URL notation: jdbc:subprotocol,other stuff. such as Mysql:jdbc:mysql://hostname:port/databasename
Connection conn = drivermanager.getconnection (Url,user,pass);
3, use connection to create a statement object.
Statement stmt = Conn.createstatement ();
4. 1, execute the SQL statement using statement. Statement has three ways to execute SQL statements:
1) Execute () executes the Select any SQL statement--Returns a Boolean value.
2) ExecuteQuery () executes the SELECT statement--Returns the query to the result set.
3) executeupdate () is used to execute DML statements, returning an integer representing the number of records affected by the SQL tackle.
ResultSet rs = stmt.executequery (SQL);
4.2 Execute the SQL statement using PreparedStatement.
If you need to repeatedly execute a similarly structured fishing gear, you can use a placeholder (? ) parameter of the SQL statement to replace it. Use the PreparedStatement interface. You can use the setxxx (int index,xxx value) method to pass in the parameter values in the settings.
Example:
Import Java.io.FileInputStream;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import java.sql.Statement;
Import java.util.Properties;
public class Connmysql {
Private String driver;
Private String URL;
Private String user;
Private String Pass;
private void Initparam (String paramfile) throws exception{
Properties Props = new properties ();
Props.load (New FileInputStream (Paramfile));
Driver = Props.getproperty ("Driver");
url = props.getproperty ("url");
user = Props.getproperty ("user");
pass = Props.getproperty ("Pass");
}
public static void Main (string[] args) throws Exception {
Connmysql ed = new Connmysql ();
Ed.initparam ("Mysql.ini");
Class.forName (Ed.driver);
Try
Connection conn = drivermanager.getconnection (Ed.url,ed.user,ed.pass);
Statement stmt = Conn.createstatement ();
PreparedStatement pstmt = conn.preparestatement ("INSERT into S1 values (?,?)");
)
{
for (int i = 0; i <; i++) {
Pstmt.setint (1, i+5);
Pstmt.setint (2,i);
Pstmt.executeupdate ();
}
}
}
}
5, you can use the Mysql.ini configuration file instead of the Driver,url,user,pass value. This file is going to move it into the project. Instead of moving into the SRC directory.
6, use Try () {}
Starting with the Java 7 Build 105 version, the Java 7 compiler and runtime environment supports the new try-with-resources statement, called ARM block (Automatic Resource Management), automatic resource management.
The data flow is automatically closed after the try has been executed, provided that these closed resources must implement the Java.lang.AutoCloseable interface.
Second, compare statement and PreparedStatement.
1,preparedstatement precompiled SQL statements for better performance.
2,preparedstatement without "stitching" SQL statements, programming is simpler.
3,preparedstatement can prevent SQL statement injection.
Java JDBC Notes collation