Java Connecting to Database Tools class and JDBC with JDBC connection MySQL Data sample _java

Source: Internet
Author: User

This tool class is simple to use, instantiate direct call on it, we can also easily according to their own needs in the inside to add their own functions

Copy Code code as follows:

Package com.lanp.ajax.db;

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

/**
* The tool class that connects the database, is defined as not inheritable and is private access
*/
Public final class Dbutils {
private static String URL = "Jdbc:mysql://localhost:3306/mydb";
private static String user = "root";
private static String PSW = "root";

private static Connection Conn;

static {
try {
Class.forName ("Com.mysql.jdbc.Driver");
catch (ClassNotFoundException e) {
E.printstacktrace ();
throw new RuntimeException (e);
}
}

Private Dbutils () {

}

/**
* Get the connection to the database
* @return Conn
*/
public static Connection getconnection () {
if (NULL = = conn) {
try {
conn = drivermanager.getconnection (URL, user, PSW);
catch (SQLException e) {
E.printstacktrace ();
throw new RuntimeException (e);
}
}
Return conn;
}

/**
* Releasing Resources
* @param Conn
* @param pstmt
* @param RS
*/
public static void Closeresources (Connection conn,preparedstatement pstmt,resultset rs) {
if (null!= RS) {
try {
Rs.close ();
catch (SQLException e) {
E.printstacktrace ();
throw new RuntimeException (e);
finally {
if (null!= pstmt) {
try {
Pstmt.close ();
catch (SQLException e) {
E.printstacktrace ();
throw new RuntimeException (e);
finally {
if (NULL!= conn) {
try {
Conn.close ();
catch (SQLException e) {
E.printstacktrace ();
throw new RuntimeException (e);
}
}
}
}
}
}
}
}




Here's a simple example of a JDBC-driven link to a MySQL database that you can use with the tools above

Using JDBC driver to link MySQL data is actually very simple, the first to download a "Mysql-connector-java-5.1.20-bin.jar" driver package. and unzip to the corresponding directory! 5.1.20 is the version number so far this is the latest version!

First, if you are developing on a command-line basis, you need to add Mysql-connector-java-5.1.2.0-bin.jar to the classpath of the system. How to add to the classpath I would like to say that we should understand it.

Second, if you are using the Eclipse development tool, configure "Java build Path" and specific actions "click on Eclipse's Project->properties->java builds path-> Libraries "Now click on the right side of the Add External JARs and select the Mysql-connector-java-5.1.2.0-bin.jar driver click to open to complete the configuration."

The following is the example code that Java uses to connect MySQL data with jdbc:

Copy Code code as follows:

Import java.sql.*;

public class Connectmysql {
public static void Main (string[] args) {
String Driver = "Com.mysql.jdbc.Driver";
String url = "Jdbc:mysql://192.168.1.112:3306/linksystem";
String user = "root";
String password = "blog.micxp.com";
try {
Class.forName (driver);
Connection conn = drivermanager.getconnection (URL, user, password);
if (!conn.isclosed ()) {
SYSTEM.OUT.PRINTLN ("Succeeded connecting to the database!");
Statement Statement = Conn.createstatement ();
String sql = "SELECT * from Flink_list";
ResultSet rs = statement.executequery (SQL);
String name;
while (Rs.next ()) {

Name = Rs.getstring ("Link_name");
SYSTEM.OUT.PRINTLN (name);
}
}
catch (Exception e) {
E.printstacktrace ();
}
}
}

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.