Java USES JDBC to connect to database utility classes and JDBC to mysql data samples

  • 2020-04-01 03:10:19
  • OfStack

This tool class is simple to use, instantiation direct call can be, you can also conveniently according to their own needs to add their own function


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;

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() {

 }

 
 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;
 }

 
 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 is a simple example of using a JDBC driver to link to a Mysql database, which you can use with the above tools

Using the JDBC driver to link to Mysql data is easy. The first step is to download a driver package called "mysql-connector-java-5.1.20-bin.jar." And unzip to the appropriate directory! 5.1.20 is the version number so far this is the latest version!

First, if you are developing from the command line, add mysql-connector-java-5.1.2.0-bin-jar to your system's CLASSPATH. I don't think you know how to add it to your CLASSPATH.

Second, if you are using the Eclipse development tool, you should also configure the "Java Build Path", the specific action "click Eclipse's Project-> The Properties - > The Java Build Path - > Libraries "now in the window to view it, click on the Add External url on the right; Then select mysql-connector-java-5.1.2.0-bin-jar driver and click open to complete the configuration.

The following is the instance code for Java to connect to Mysql data using JDBC:


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();   
        }   
    }   
}   


Related articles: