java MySQL templates

Source: Internet
Author: User

Java MySQL template, very elegant. It also takes into account both performance PreparedStatement and security (anti-SQL injection). For relatively simple database operations basically meet the requirements.

Package DAO;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;

public class Basedao {
public static final String DRIVER = "Oracle.jdbc.driver.OracleDriver";
public static final String URL = "Jdbc:oracle:thin: @localhost: 1521:ORCL";
public static final String USERNAME = "Ma";
public static final String PASSWORD = "Malei";

Connection Connection = null;
PreparedStatement preparedstatement = null;
ResultSet ResultSet = null;

Public Connection getconnection () throws Exception {
Class.forName (DRIVER);
Connection = Drivermanager.getconnection (Url,username,password);
return connection;
}

Public ResultSet executeQuery (String sql) throws Exception {
Connection = This.getconnection ();
PreparedStatement = connection.preparestatement (sql);
ResultSet = Preparedstatement.executequery ();
return resultSet;

}

public int executeupdate (String sql,object[] obj) throws Exception {
Connection = This.getconnection ();
PreparedStatement = connection.preparestatement (sql);
for (int i =0;i<obj.length;i++) {
Preparedstatement.setobject (i+1, obj[i]);
}
return Preparedstatement.executeupdate ();
}

public void CloseAll () throws Exception {
if (null! = ResultSet) {
Resultset.close ();
}
if (null! = PreparedStatement) {
Preparedstatement.close ();
}
if (null! = connection) {
Connection.close ();
}
}
}

Related Article

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.