当前位置: 首页 > news >正文

dlink nas建设网站爱站工具下载

dlink nas建设网站,爱站工具下载,成都中小企业网站建设,mui做网站前言 其实学Mybatis前就该学了,但是寻思目前主流框架都是用mybatis和mybatis-plus就没再去看,结果在代码审计中遇到了很多cms是使用jdbc的因此还是再学一下吧。 第一个JDBC程序 sql文件 INSERT INTO users(id, NAME, PASSWORD, email, birthday) VAL…

前言

其实学Mybatis前就该学了,但是寻思目前主流框架都是用mybatis和mybatis-plus就没再去看,结果在代码审计中遇到了很多cms是使用jdbc的因此还是再学一下吧。

第一个JDBC程序

sql文件

INSERT INTO `users`(`id`, `NAME`, `PASSWORD`, `email`, `birthday`) VALUES (1, 'zhansan', '123456', 'zs@sina.com', '1980-12-04');
INSERT INTO `users`(`id`, `NAME`, `PASSWORD`, `email`, `birthday`) VALUES (2, 'lisi', '123456', 'lisi@sina.com', '1981-12-04');
INSERT INTO `users`(`id`, `NAME`, `PASSWORD`, `email`, `birthday`) VALUES (3, 'wangwu', '123456', 'wangwu@sina.com', '1979-12-04');

HelloJDBC

import java.sql.*;public class HelloJDBC {public static void main(String[] args) throws ClassNotFoundException, SQLException {//1. 加载驱动Class.forName("com.mysql.jdbc.Driver");//2. 用户信息和urlString url = "jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf8&useSSL=true";String name = "root";String password = "123456";//3. 连接数据库 connection是数据库对象Connection connection = DriverManager.getConnection(url, name, password);//4. 执行sql的对象 statement是执行sql的对象Statement statement = connection.createStatement();//5. 用statement对象执行sql语句String sql = "select * from users";ResultSet resultSet = statement.executeQuery(sql);while (resultSet.next()){System.out.println("id:"+resultSet.getObject("id")+",name:"+resultSet.getObject("name")+",password:"+resultSet.getObject("password"));System.out.println("=============================");}//6.释放资源resultSet.close();statement.close();connection.close();}
}

Statement对象

工具类

package utils;import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;public class JdbcUtils {public static Connection connection() throws ClassNotFoundException, SQLException, IOException {InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");Properties properties = new Properties();properties.load(in);String driver = properties.getProperty("driver");String url = properties.getProperty("url");String username = properties.getProperty("username");String password = properties.getProperty("password");Class.forName(driver);return DriverManager.getConnection(url,username,password);}public static void relese(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {if (resultSet!=null){resultSet.close();}if (statement!=null){statement.close();}if (connection!=null){connection.close();}}
}

Demo

import utils.JdbcUtils;import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class JdbcStatement {public static void main(String[] args) throws Exception {
//        query();insert();}public static void query() throws SQLException, ClassNotFoundException, IOException{Connection connection = JdbcUtils.connection();String sql = "select * from users";Statement statement = connection.createStatement();ResultSet resultSet = statement.executeQuery(sql);while(resultSet.next()){System.out.println("id:"+resultSet.getObject("id"));}JdbcUtils.relese(resultSet,statement,connection);}public static void insert() throws Exception{Connection connection = JdbcUtils.connection();String sql = "insert into users values(4,'Sentiment',123456,'Sentiment@qq.com','1980-12-04')";Statement statement = connection.createStatement();int i = statement.executeUpdate(sql);System.out.println(i);JdbcUtils.relese(null,statement,connection);}
}

查询用executeQuery(),增、删、改用executeUpdate()

PreparedStatement对象

用statement的话会有sql注入问题,因此可以用preparedstatement进行预处理来进行防御

主要是通过占位符来执行查询语句

public class JdbcPreparedStatement {public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {Connection connection = JdbcUtils.connection();String sql = "insert into users values(?,?,?,null,null)";PreparedStatement ps = connection.prepareStatement(sql);ps.setInt(1,5);ps.setString(2,"Sentiment");ps.setInt(3,123456);ps.execute();}
}

操作事务

mybatis中学过,不过连含义都忘了。。。。

其实就是执行语句时要不都成功执行,一个不能执行则全部都不执行

主要就是关闭自动提交事务

connection.setAutoCommit(false); //开启事务

Demo

  PreparedStatement ps = null;Connection connection = null;try {connection = JdbcUtils.connection();//关闭数烟库的自动提交,自动会开启事务connectionconnection.setAutoCommit(false); //开启事务String sql1 = "update users set name = 'Sentiment' where id=3";ps = connection.prepareStatement(sql1);ps.executeUpdate();int x = 1 / 0;String sql2 = "update users set name = 'Sentiment' where id=1";ps = connection.prepareStatement(sql2);ps.executeUpdate();connection.commit();System.out.println("Success!");}catch (SQLException e){connection.rollback();      //执行失败后,事务回滚}finally {JdbcUtils.relese(null,ps,connection);}}

数据库连接池

在上述工具类中,是通过以下方法来获取配置文件参数,并连接连接池

String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
Class.forName(driver);
return DriverManager.getConnection(url,username,password);

而我们可以用开源的数据库连接池如DBCP、C3P0、Druid等

使用了这些数据库连接池之后,我们在项目开发中就不需要编写连接数据库的代码了!

DBCP

<dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>1.4</version>
</dependency><dependency><groupId>commons-pool</groupId><artifactId>commons-pool</artifactId><version>1.5.4</version>
</dependency>

配置文件

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=123456#<!-- 初始化连接 -->
initialSize=10#最大连接数量
maxActive=50#<!-- 最大空闲连接 -->
maxIdle=20#<!-- 最小空闲连接 -->
minIdle=5#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60-->
maxWait=60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:【属性名=property;】
#注意:"user""password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true;characterEncoding=utf8#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true#driver default 指定由连接池所创建的连接的只读(read-only)状态。
#如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)
defaultReadOnly=true#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_COMMITTED

Demo

使用DBCP后,utils就可以简化为:

public static Connection connection() throws Exception {InputStream in = DBCP_Utils.class.getClassLoader().getResourceAsStream("dbcp.properties");Properties properties = new Properties();properties.load(in);//创建数据源DataSource dataSource = BasicDataSourceFactory.createDataSource(properties);return dataSource.getConnection();
}

读取配置文件,交给数据源即可

C3P0

这个更简单

<dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.5</version>
</dependency>
<dependency><groupId>com.mchange</groupId><artifactId>mchange-commons-java</artifactId><version>0.2.19</version>
</dependency>

配置文件

这里设置了两个数据源:

<default-config>:默认值创建数据源时不需要形参,ComboPooledDataSource ds=new ComboPooledDataSource();

<named-config name="MySQL">: 非默认要指定数据源,ComboPooledDataSource ds=new ComboPooledDataSource(“MySQL”);

<?xml version="1.0" encoding="UTF-8"?><c3p0-config><!--c3p0的缺省(默认)配置如果在代码中"ComboPooledDataSource ds=new ComboPooledDataSource();"这样写就表示使用的是c3p0的缺省(默认)--><default-config><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=true</property><property name="user">root</property><property name="password">123456</property><property name="acquiredIncrement">5</property><property name="initialPoolSize">10</property><property name="minPoolSize">5</property><property name="maxPoolSize">20</property></default-config><!--c3p0的命名配置如果在代码中"ComboPooledDataSource ds=new ComboPooledDataSource("MySQL");"这样写就表示使用的是mysql的缺省(默认)--><named-config name="MySQL"><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=true</property><property name="user">root</property><property name="password">123456</property><property name="acquiredIncrement">5</property><property name="initialPoolSize">10</property><property name="minPoolSize">5</property><property name="maxPoolSize">20</property></named-config></c3p0-config>

Demo

xml文件默认能读取到

public static Connection connection() throws Exception {ComboPooledDataSource dataSource=new ComboPooledDataSource();return dataSource.getConnection();
}

自带日志

在这里插入图片描述


文章转载自:
http://classification.c7617.cn
http://halfback.c7617.cn
http://polyarchy.c7617.cn
http://week.c7617.cn
http://megohm.c7617.cn
http://metafile.c7617.cn
http://damnable.c7617.cn
http://expel.c7617.cn
http://letting.c7617.cn
http://telanthropus.c7617.cn
http://hernia.c7617.cn
http://mesocardium.c7617.cn
http://ilex.c7617.cn
http://intomb.c7617.cn
http://sharecrop.c7617.cn
http://mind.c7617.cn
http://urdu.c7617.cn
http://laterality.c7617.cn
http://hardcover.c7617.cn
http://seismological.c7617.cn
http://absinth.c7617.cn
http://bowhead.c7617.cn
http://pinwheel.c7617.cn
http://revelational.c7617.cn
http://quadrillion.c7617.cn
http://leisureliness.c7617.cn
http://necessitating.c7617.cn
http://dodgem.c7617.cn
http://dish.c7617.cn
http://anxiety.c7617.cn
http://bakemeat.c7617.cn
http://unmutilated.c7617.cn
http://revisional.c7617.cn
http://albert.c7617.cn
http://ikaria.c7617.cn
http://flocci.c7617.cn
http://catalpa.c7617.cn
http://toplofty.c7617.cn
http://nastalik.c7617.cn
http://campership.c7617.cn
http://erythrophobia.c7617.cn
http://atwitter.c7617.cn
http://homoecious.c7617.cn
http://deliverer.c7617.cn
http://unreadable.c7617.cn
http://martyry.c7617.cn
http://cmb.c7617.cn
http://sagamore.c7617.cn
http://enunciation.c7617.cn
http://semibarbarous.c7617.cn
http://changkiang.c7617.cn
http://groundsill.c7617.cn
http://sweetly.c7617.cn
http://pindar.c7617.cn
http://nasology.c7617.cn
http://tenorite.c7617.cn
http://involantary.c7617.cn
http://flotsan.c7617.cn
http://capable.c7617.cn
http://cymbalom.c7617.cn
http://legitimacy.c7617.cn
http://bacteriolysis.c7617.cn
http://whitlow.c7617.cn
http://callow.c7617.cn
http://drive.c7617.cn
http://telerecording.c7617.cn
http://daphnia.c7617.cn
http://broncobuster.c7617.cn
http://drummer.c7617.cn
http://reeducation.c7617.cn
http://battlefront.c7617.cn
http://taurine.c7617.cn
http://chasteness.c7617.cn
http://foolish.c7617.cn
http://snorer.c7617.cn
http://pluckily.c7617.cn
http://dulcification.c7617.cn
http://icr.c7617.cn
http://pharmacological.c7617.cn
http://dadaist.c7617.cn
http://beeves.c7617.cn
http://entad.c7617.cn
http://crosier.c7617.cn
http://alcoranist.c7617.cn
http://pilonidal.c7617.cn
http://cit.c7617.cn
http://notoungulate.c7617.cn
http://tsetse.c7617.cn
http://polyurethane.c7617.cn
http://disject.c7617.cn
http://coronium.c7617.cn
http://circumstantial.c7617.cn
http://horseless.c7617.cn
http://sputum.c7617.cn
http://rosa.c7617.cn
http://traitorous.c7617.cn
http://penultimate.c7617.cn
http://brownette.c7617.cn
http://gwine.c7617.cn
http://pending.c7617.cn
http://www.zhongyajixie.com/news/101652.html

相关文章:

  • 宠物网站开发功能需求品牌宣传推广策划方案
  • 京广桥做网站的公司深圳正规seo
  • 加盟网站建设百度云盘资源
  • 犀牛云网站做的怎么样营销型网站建设的5大技巧
  • 厦门网站建设定制多少钱湖南seo优化排名
  • 网站后台视频免费网络推广工具
  • 石家庄城乡建设管理局网站百度快速优化排名软件
  • 北京电子商务网站制作软文网站
  • com域名的网址有哪些网站为什么要seo?
  • 物理机安装虚拟机做网站定制网站建设电话
  • 石家庄网站建设价格sem竞价广告
  • 全套免费代码大全聊石家庄seo
  • 青海网站开发建设win7优化大师官方网站
  • 校园门户网站解决方案苏州网站建设开发公司
  • 老域名做网站好吗seo的理解
  • 电子商务网站建设方案欧洲站fba
  • 网站seo设置是什么意思公司做网站需要多少钱
  • 个人网站可以做健康付费知识小程序开发平台
  • wordpress 主题 html5 左右滑动切换文章站群优化公司
  • 网络服务合同范本免费百度seo建议
  • 建设银行网站色调湖南seo博客seo交流
  • 雄安网站建设多少钱网络营销策划推广公司
  • 深圳著名设计网站湛江百度seo公司
  • 番禺人才网车床工铣床工招聘济南网站优化公司哪家好
  • 手机上如何做网站换友情链接的网站
  • 做网站图标的软件网络营销是以什么为基础
  • 网站与网络的区别网站宣传
  • 建设网站桫椤在室内能竞价推广账户竞价托管
  • 焦作网站建设哪家好今日新闻快讯
  • 高端网站建设苏州前端seo搜索引擎优化