Java Connection MySQL Database learning example _java

Source: Internet
Author: User

Copy Code code as follows:

Package sns.team6.util;

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

/**
* Link the Database tool class
*
* @author Xu Rui
*
*/
public class DBHelper {

MySQL Drive path
Private static final String Driver = "Com.mysql.jdbc.Driver";
Connection path for database
Private static final String URL = "JDBC:MYSQL://LOCALHOST:3306/SNSTEAM6";

 /**
  * Objects that connect to the database
  *
  * @return
  */
 public static Connection Getconnection () {
  connection conn = null;
  try {
   class.forname (driver);
   conn = drivermanager.getconnection (URL, "root", "root");
  } catch (Exception e) {
   e.printstacktrace ();
 &NBSP}
  return conn;
 .}

 /**
  * Turn off Open resources
  *
  * @param conn
  * @param PST
  * @param rst
&NB Sp */
 public static void Closeinfo (Connection conn, PreparedStatement PST,
   resultset rst) {
  try {
   if (rst!= null) {
    rst.close ();
    rst = null;
   }
   if (PST!= null) {
    pst.close ();
    pst = null;
   }
   IF (conn!= null) {
    conn.close ();
    conn = null;
   }
  } catch (Exception e) {
   e.printstacktrace ();
 &NBSP}
 }

/**
* Get result set object
*
* @param sql
* @param params
* @return
*/
public static ResultSet ResultSet (String sql, object[] params) {

The linked object of the database
Connection conn = null;
The Operation object of the database
PreparedStatement PST = NULL;
Result Object
ResultSet rst = null;
try {

The linked object of the database
conn = Dbhelper.getconnection ();
The Operation object of the database
PST = conn.preparestatement (SQL);
Determine if there are parameters
if (params!= null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
Assigning a value to an Action object
Pst.setobject (i + 1, params[i]);
}
}
Get the result object
rst = Pst.executequery ();
catch (SQLException e) {
rst = null;
E.printstacktrace ();
}
return rst;
}

/**
* Get the added result object
*
* @param sql
* @param params
* @return
*/
public static Boolean result (String sql, object[] params) {

Boolean flag = false;
The linked object of the database
Connection conn = null;
The Operation object of the database
PreparedStatement PST = NULL;
try {
The linked object of the database
conn = Dbhelper.getconnection ();
The Operation object of the database
PST = conn.preparestatement (SQL);
Determine if there are parameters
if (params!= null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
Assigning a value to an Action object
Pst.setobject (i + 1, params[i]);
}
}
Gets the result object, which is the int type, indicating the number of rows executed successfully
int row = Pst.executeupdate ();
if (Row > 0) {
If greater than 0 indicates successful execution
Flag = true;
}
catch (SQLException e) {
Flag = false;
E.printstacktrace ();
}
return flag;
}
}

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.