java使用jdbc連接數(shù)據(jù)庫工具類和jdbc連接mysql數(shù)據(jù)示例
這個(gè)工具類使用簡單,實(shí)例化直接調(diào)用就可以了,大家還可以方便的根據(jù)自己的需要在里面增加自己的功能
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;
/**
* 連接數(shù)據(jù)庫的工具類,被定義成不可繼承且是私有訪問
*/
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() {
}
/**
* 獲取數(shù)據(jù)庫的連接
* @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;
}
/**
* 釋放資源
* @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);
}
}
}
}
}
}
}
}
下面為大家找到一個(gè)使用JDBC驅(qū)動(dòng)鏈接Mysql數(shù)據(jù)庫的簡單示例,可以和上面的工具一起參考使用
利用JDBC驅(qū)動(dòng)鏈接Mysql數(shù)據(jù)其實(shí)很簡單的,第一要下載一個(gè)名為 “mysql-connector-java-5.1.20-bin.jar” 驅(qū)動(dòng)包。并解壓到相應(yīng)的目錄!5.1.20是版 本號(hào)到目前為止這個(gè)是最新的版本!
第一、如果你是在命令行方式下開發(fā),需要把mysql-connector-java-5.1.2.0-bin.jar 添加到系統(tǒng)的CLASSPATH中。怎么加到CLASSPATH中我想不要講了大家也應(yīng)懂的吧。
第二、如果你是用Eclipse開發(fā)工具的話,還要配置一下 "Java Build Path"、具體的操作“點(diǎn)擊Eclipse的Project->Properties->Java Build Path->Libraries” 現(xiàn)在在看以的窗口中點(diǎn)擊右邊的Add External JARs 然后選擇mysql-connector-java-5.1.2.0-bin.jar驅(qū)動(dòng) 點(diǎn)擊打開就完成了配置。
下面就是Java利用JDBC連接Mysql數(shù)據(jù)的實(shí)例代碼:
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();
}
}
}
相關(guān)文章
Java并發(fā)編程之CountDownLatch的使用
CountDownLatch是一個(gè)倒數(shù)的同步器,常用來讓一個(gè)線程等待其他N個(gè)線程執(zhí)行完成再繼續(xù)向下執(zhí)行,本文主要介紹了CountDownLatch的具體使用方法,感興趣的可以了解一下2023-05-05詳解Java Fibonacci Search斐波那契搜索算法代碼實(shí)現(xiàn)
這篇文章主要介紹了詳解Java Fibonacci Search斐波那契搜索算法代碼實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Java面試之如何實(shí)現(xiàn)10億數(shù)據(jù)判重
當(dāng)數(shù)據(jù)量比較大時(shí),使用常規(guī)的方式來判重就不行了,所以這篇文章小編主要來和大家介紹一下Java實(shí)現(xiàn)10億數(shù)據(jù)判重的相關(guān)方法,希望對(duì)大家有所幫助2024-02-02springboot整合redis過期key監(jiān)聽實(shí)現(xiàn)訂單過期的項(xiàng)目實(shí)踐
現(xiàn)在各種電商平臺(tái)都有自己的訂單過期時(shí)間設(shè)置,那么如何設(shè)置訂單時(shí)間過期呢,本文主要介紹了springboot整合redis過期key監(jiān)聽實(shí)現(xiàn)訂單過期的項(xiàng)目實(shí)踐,感興趣的可以了解一下2023-12-12IDEA創(chuàng)建MyBatis配置文件模板的方法步驟
這篇文章主要介紹了IDEA創(chuàng)建MyBatis配置文件模板的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Hibernate延遲加載原理與實(shí)現(xiàn)方法
這篇文章主要介紹了Hibernate延遲加載原理與實(shí)現(xiàn)方法,較為詳細(xì)的分析了Hibernate延遲加載的概念,原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-03-03Springboot實(shí)例講解實(shí)現(xiàn)專業(yè)材料認(rèn)證管理系統(tǒng)流程
這是一個(gè)基于java的畢業(yè)設(shè)計(jì)項(xiàng)目,畢設(shè)課題為springboot框架的知識(shí)產(chǎn)權(quán)服務(wù)平臺(tái)系統(tǒng),是一個(gè)采用b/s結(jié)構(gòu)的javaweb項(xiàng)目,需要的朋友可以參考下2022-06-06Mybatis批量插入返回成功的數(shù)目實(shí)例
這篇文章主要介紹了Mybatis批量插入返回成功的數(shù)目實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12