湖南网站建设公司排名,中国建设银行网站多少,网络推广文案有哪些,系统架构有哪几种一、准备普通项目如果创建的是普通的Java项目#xff0c;我们需要去maven仓库下载jdbc驱动包然导入项目中就能使用#xff0c;具体步骤详见MySQL数据库之Java中如何使用数据库【JDBC编程】maven项目如果创建的项目是maven项目#xff0c;我们只需在pom.xml文件里引入一组依赖…一、准备普通项目如果创建的是普通的Java项目我们需要去maven仓库下载jdbc驱动包然导入项目中就能使用具体步骤详见MySQL数据库之Java中如何使用数据库【JDBC编程】maven项目如果创建的项目是maven项目我们只需在pom.xml文件里引入一组依赖即可dependencies!--jdbc--!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.49/version/dependency/dependencies引入依赖后点击右侧的maven刷新即可创建类导入代码import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class JDBCDemo {private static DataSource dataSource;private static String database;private static String password;private static int port 3306; //如果修改了端口号提供设置/*** 提供构造方法 传入操作的数据库名称 与 数据库的密码* param database 数据库名* param password 数据库密码*/public JDBCDemo(String database,String password) throws SQLException {if (password null){password ;}if (database null || database ){throw new SQLException(数据库名为空);}this.database database;this.password password;}/*** 如果修改了MySQL的端口号就进行设置* param port*/public void setPort(int port){this.port port;}// 封装CRUDprivate int SQL(String sql){int n 0;Connection connection null;PreparedStatement statement null;try {// 建立连接connection DBUtil.getConnect();// 构造请求statement connection.prepareStatement(sql);// 发送请求n statement.executeUpdate();} catch (SQLException e) {e.printStackTrace();} finally {// 释放资源DBUtil.close(connection,statement,null);}return n;}public boolean insert(String sql) {return SQL(sql) 1 ? true : false;}public boolean delete(String sql){return SQL(sql) 1 ? true : false;}public boolean update(String sql) {return SQL(sql) 1 ? true : false;}/**** param map 键值对 字段名字段对应的Java类型* param sql 对应的查询sql语句* return 返回M键值对 字段名值需要根据具体类型进行转换*/public HashMapString,String select(MapString,String map, String sql){HashMapString,String result new HashMap();Connection connection null;PreparedStatement statement null;ResultSet resultSet null;try {// 建立连接connection DBUtil.getConnect();// 构造sqlstatement connection.prepareStatement(sql);// 处理响应resultSet statement.executeQuery();SetMap.EntryString,String entry map.entrySet();while (resultSet.next()){for (Map.EntryString ,String x : entry) {switch (x.getKey()){case int:int i resultSet.getInt(x.getValue());result.put(x.getValue(),String.valueOf(i));break;case double:double d resultSet.getDouble(x.getValue());result.put(x.getValue(),String.valueOf(d));break;case float:float f resultSet.getFloat(x.getValue());result.put(x.getValue(),String.valueOf(f));break;case byte:byte b resultSet.getByte(x.getValue());result.put(x.getValue(),String.valueOf(b));break;case char:String c resultSet.getString(x.getValue());result.put(x.getValue(),String.valueOf(c));break;case String:result.put(x.getValue(),x.getKey());break;case long:long l resultSet.getLong(x.getValue());result.put(x.getValue(), String.valueOf(l));break;case short:short s resultSet.getShort(x.getValue());result.put(x.getValue(),String.valueOf(s));break;default:throw new Exception(类型错误);}}}} catch (SQLException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} finally {DBUtil.close(connection,statement,resultSet);}return result;}/*** 封装与数据库连接相关的操作*/private static class DBUtil {/*** 单例模式封装数据源* return*/private static DataSource getInstance(){if (dataSource null){synchronized (JDBCDemo.class){if (dataSource null){dataSource new MysqlDataSource();((MysqlDataSource)dataSource).setURL(jdbc:mysql://127.0.0.1: JDBCDemo.port / JDBCDemo.database ?characterEncodingutf8useSSLfalse);((MysqlDataSource)dataSource).setUser(root);((MysqlDataSource)dataSource).setPassword(JDBCDemo.password);}}}return dataSource;}/*** 与数据库建立连接* return*/private static Connection getConnect() throws SQLException {return getInstance().getConnection();}/*** 释放资源* param connection* param statement* param resultSet*/private static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) {if(resultSet ! null){try {resultSet.close();} catch (SQLException e) {e.printStackTrace();}}if(statement ! null){try {statement.close();} catch (SQLException e) {e.printStackTrace();}}if(connection ! null){try {connection.close();} catch (SQLException e) {e.printStackTrace();}}}}
}
二、API创建实例JDBCDemo传入要操作的数据库的库名以及数据库密码如果修改过MySQL的端口号调用setPort(int)方法进行设置进行数据库增删改查代码示例如下方法名参数说明insertString sql只需传入增加数据的SQL语句即可updateString sql只需传入修改的SQL语句即可deleteString sql只需传入删除的SQL语句即可inselectMapString,String map, String sqlmap是一组键值对键是要查询数据库表的字段名值是其对应的Java类型如varchar(20)对应String)public static void main(String[] args) throws SQLException {// 1.创建实例传入数据库名此处的数据库是指create database创建的数据库里与密码JDBCDemo jdbcDemo new JDBCDemo(student,这里是数据库密码);// 2.如果修改过数据库的端口号可调用一下方法修改,默认是没有修改的jdbcDemo.setPort(3306);// 3.添加数据String name 李四;int age 10;jdbcDemo.insert(insert into student values( name , age ));jdbcDemo.update(update student set age 11 where name李四);jdbcDemo.delete(delete from student where name张三);// 获取树数据HashMapString,String tableName new HashMap();map.put(name,String);map.put(age,int);HashMapString,String ret jdbcDemo.select(map,select * from student);int studentAge Integer.parseInt(map.get(age));}
文章转载自: http://www.morning.trnl.cn.gov.cn.trnl.cn http://www.morning.plqsc.cn.gov.cn.plqsc.cn http://www.morning.poapal.com.gov.cn.poapal.com http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.dsncg.cn.gov.cn.dsncg.cn http://www.morning.yxzfl.cn.gov.cn.yxzfl.cn http://www.morning.fnwny.cn.gov.cn.fnwny.cn http://www.morning.wqfj.cn.gov.cn.wqfj.cn http://www.morning.ffbl.cn.gov.cn.ffbl.cn http://www.morning.prgyd.cn.gov.cn.prgyd.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.zgdnd.cn.gov.cn.zgdnd.cn http://www.morning.hxcuvg.cn.gov.cn.hxcuvg.cn http://www.morning.thbnt.cn.gov.cn.thbnt.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.dxgt.cn.gov.cn.dxgt.cn http://www.morning.mnccq.cn.gov.cn.mnccq.cn http://www.morning.bpds.cn.gov.cn.bpds.cn http://www.morning.qfdyt.cn.gov.cn.qfdyt.cn http://www.morning.rmkyb.cn.gov.cn.rmkyb.cn http://www.morning.datadragon-auh.cn.gov.cn.datadragon-auh.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.cbpmq.cn.gov.cn.cbpmq.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.kqrql.cn.gov.cn.kqrql.cn http://www.morning.xkzmz.cn.gov.cn.xkzmz.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.qrzwj.cn.gov.cn.qrzwj.cn http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.dnqliv.cn.gov.cn.dnqliv.cn http://www.morning.fkfyn.cn.gov.cn.fkfyn.cn http://www.morning.sgnjg.cn.gov.cn.sgnjg.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.ryxbz.cn.gov.cn.ryxbz.cn http://www.morning.ykbgs.cn.gov.cn.ykbgs.cn http://www.morning.rwbh.cn.gov.cn.rwbh.cn http://www.morning.lsgjf.cn.gov.cn.lsgjf.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.kyfnh.cn.gov.cn.kyfnh.cn http://www.morning.rkyw.cn.gov.cn.rkyw.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn http://www.morning.ghzfx.cn.gov.cn.ghzfx.cn http://www.morning.nhrkc.cn.gov.cn.nhrkc.cn http://www.morning.lltdf.cn.gov.cn.lltdf.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.tnjkg.cn.gov.cn.tnjkg.cn http://www.morning.gblrn.cn.gov.cn.gblrn.cn http://www.morning.gtcym.cn.gov.cn.gtcym.cn http://www.morning.ltrz.cn.gov.cn.ltrz.cn http://www.morning.rfpq.cn.gov.cn.rfpq.cn http://www.morning.yfmwg.cn.gov.cn.yfmwg.cn http://www.morning.krhkb.cn.gov.cn.krhkb.cn http://www.morning.nxfwf.cn.gov.cn.nxfwf.cn http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.xtgzp.cn.gov.cn.xtgzp.cn http://www.morning.hbnwr.cn.gov.cn.hbnwr.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.lsqmb.cn.gov.cn.lsqmb.cn http://www.morning.bsjpd.cn.gov.cn.bsjpd.cn http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn http://www.morning.bmsqq.cn.gov.cn.bmsqq.cn http://www.morning.dpjtn.cn.gov.cn.dpjtn.cn http://www.morning.mxlwl.cn.gov.cn.mxlwl.cn http://www.morning.shinezoneserver.com.gov.cn.shinezoneserver.com http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.rzczl.cn.gov.cn.rzczl.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.hpjpy.cn.gov.cn.hpjpy.cn http://www.morning.wnjwb.cn.gov.cn.wnjwb.cn http://www.morning.ahscrl.com.gov.cn.ahscrl.com http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.nhpgm.cn.gov.cn.nhpgm.cn http://www.morning.ygkk.cn.gov.cn.ygkk.cn http://www.morning.hyhqd.cn.gov.cn.hyhqd.cn