博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jdbc链接数据库
阅读量:7254 次
发布时间:2019-06-29

本文共 6224 字,大约阅读时间需要 20 分钟。

虽然简单,但是有时候只能留个印象。

public class JdbcUtil {    static String url = null;    static String username = null;    static String password = null;    static String driverClass = null;    static {        try {            Properties properties = new Properties();            InputStream in = JdbcUtil.class                    .getResourceAsStream("/jdbc.properties");            properties.load(in);            url = properties.getProperty("url");            username = properties.getProperty("username");            password = properties.getProperty("password");            driverClass = properties.getProperty("driver");                        System.out.println(password);            Class.forName(driverClass);        } catch (Exception e) {            e.printStackTrace();            System.out.println("驱动加载失败!");            throw new RuntimeException(e);        }    }    public static Connection getConnection() {        try {            Connection conn = DriverManager.getConnection(url, username, password);            return conn;        } catch (SQLException e) {            e.printStackTrace();            throw new RuntimeException(e);        }    }    public static void close(Connection conn, Statement st, ResultSet rs) {        if (conn != null) {            try {                conn.close();            } catch (Exception e) {                throw new RuntimeException(e);            }        }        if (st != null) {            try {                st.close();            } catch (Exception e) {                throw new RuntimeException(e);            }        }        if (rs != null) {            try {                rs.close();            } catch (Exception e) {                throw new RuntimeException(e);            }        }    }    public static void close(Connection conn, Statement st) {        if (conn != null) {            try {                conn.close();            } catch (Exception e) {                throw new RuntimeException(e);            }        }        if (st != null) {            try {                st.close();            } catch (Exception e) {                throw new RuntimeException(e);            }        }    }         /*     * 释放资源     */    public static void releaseResources(ResultSet resultSet,            Statement statement, Connection connection) {        try {            if (resultSet != null)                resultSet.close();        } catch (SQLException e) {            e.printStackTrace();        } finally {            resultSet = null;            try {                if (statement != null)                    statement.close();            } catch (SQLException e) {                e.printStackTrace();            } finally {                statement = null;                try {                    if (connection != null)                        connection.close();                } catch (SQLException e) {                    e.printStackTrace();                } finally {                    connection = null;                }            }        }    }}

 

 

import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import bean.QuackResults;//存入数据库新增加功能/** * 2017/10/21 * @author lemo * */public class DbExcute {    private Connection connection;    private Statement statement;    private ResultSet resultSet;    // 更新操作    public void update(String sql) {        try {            connection = JdbcUtil.getConnection();            statement = connection.createStatement();            // 可执行创建、修改、删除表,添加、删除、修改元组以及查询sql语句            boolean num=statement.execute(sql);            System.out.println(num);        } catch (SQLException e) {            e.printStackTrace();        } finally {            JdbcUtil.close(connection, (com.mysql.jdbc.Statement) statement,                    resultSet);        }    }    // 查询操作    public ResultSet Query(String sql) {        try {            connection = JdbcUtil.getConnection();            statement = connection.createStatement();            resultSet = statement.executeQuery(sql);        } catch (SQLException e) {            e.printStackTrace();        } finally {            JdbcUtil.releaseResources(resultSet, statement, connection);        }        return resultSet;    }    // 添加操作    public void addElement(String sql) {        update(sql);    }    public void addElement(QuackResults aQuackResults) {        /*String sqlStr = "select* from users where username=? and password=?";         connection = JdbcUtil.getConnection();        PreparedStatement aStatement=null;        try {                        aStatement=connection.prepareStatement(sqlStr);            aStatement.setString(1,"zhansgan");            aStatement.setString(2, "root");        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }*/                String sqlStr = "insert into mine_quack_results values(null,?,?,?,?,?)";         connection = JdbcUtil.getConnection();        PreparedStatement aStatement=null;        try {                        aStatement=connection.prepareStatement(sqlStr);            aStatement.setDouble(1,aQuackResults.getxData());            aStatement.setDouble(2, aQuackResults.getyData());            aStatement.setDouble(3, aQuackResults.getzData());            aStatement.setTimestamp(4,aQuackResults.getQuackTime());            aStatement.setDouble(5, aQuackResults.getQuackGrade());            System.out.println(aStatement.execute());            connection.close();        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }            }    // 删除操作    public void removeElement(String sql) {        update(sql);                    }    // 创建一个表    public void createTable(String sql) {        update(sql);    }    // 删除一个表    public void dropTable(String sql) {        update(sql);    }}

 

driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/ks?useUnicode=true&characterEncoding=utf8&allowMultiQueries=trueusername=rootpassword=root#定义初始连接数initialSize=0#定义最大连接数maxActive=20#定义最大空闲maxIdle=20#定义最小空闲minIdle=1#定义最长等待时间maxWait=60000

 

转载于:https://www.cnblogs.com/elenz/p/7808770.html

你可能感兴趣的文章
Oracle数据库备份还原工具之Expdp/IMPdp
查看>>
【转】android JNI编程 一些技巧(整理)
查看>>
MySQL之终端(Terminal)管理数据库、数据表、数据的基本操作
查看>>
各种排序算法汇总
查看>>
C#巧用Excel模版变成把Table打印出来
查看>>
SOAP 及其安全控制--转载
查看>>
JarSearch
查看>>
[Unity3D][Vuforia][IOS]vuforia在unity3d中添加自己的动态模型,识别自己的图片,添加GUI,播放视频...
查看>>
Freemodbus介绍及测试
查看>>
[转]Phantomjs实现获取网页快照并生成缩略图
查看>>
leveldb源码学习系列
查看>>
Linux 运行 apt-get install 就出现jdk installer 错误的解决方法
查看>>
Android OpenGL ES(九)绘制线段Line Segment .
查看>>
Ubuntu下安装配置JDK1.7
查看>>
转载:STM32之中断与事件---中断与事件的区别
查看>>
[裴礼文数学分析中的典型问题与方法习题参考解答]4.5.10
查看>>
设计模式(十四)单例模式(创建型)
查看>>
JAVA修饰符类型(public,protected,private,friendly)
查看>>
haxm intelx86加速模拟器的安装
查看>>
(ETW) Event Tracing for Windows 入门 (含pdf下载)
查看>>