JDBC是Java数据库连接技术,他能实现Java程序对各种数据库的访问。由一组使用Java语言编写的类和接口组成,他们位于java.sql和javax.sql中。 JDBC是一种用于执行SQL语句的java API,JDBC并不能直接访问数据库,需要依赖于数据库厂商提供的JDBC驱动程序。 JDB
JDBC是Java数据库连接技术,他能实现Java程序对各种数据库的访问。由一组使用Java语言编写的类和接口组成,他们位于java.sql和javax.sql中。
JDBC是一种用于执行SQL语句的java API,JDBC并不能直接访问数据库,需要依赖于数据库厂商提供的JDBC驱动程序。
JDBC的编程步骤如下:
1、通过Class.forName();注册驱动。
2、通过DriverManager类的getConnection()方法获得数据库连接。需要给出连接数据库的URL和数据库用户名和密码。
3、用数据库连接对象获得Statement对象,他可以用来执行SQL语句。
4、用Statement对象执行SQL语句。实质上SQL语句是数据库执行的,Statement只是负责发送SQL和接受结果。
5、处理执行结果。查询结果为ResultSet。
6、释放数据库的连接。
因为在开发和部署时可能用到不同的数据库,因此我们可以讲数据库信息写在配置文件中,让程序通过读取配置文件来获得这些信息。
下面看一个示例:
首先看数据库(这里使用Oracle数据库):
该数据库公有7个字段,id字段是主键,代表新闻编号;title字段表示新闻标题;summary字段代表新闻摘要,content代表新闻内容;createdate表示新闻创建时间;mindifydate代表新闻最终修改时间。
然后在看java项目中的配置文件。在此之前需要在项目中导入Oracle数据库的驱动包ojdbc14.jar。
jdbc.driver_class=oracle.jdbc.driver.OracleDriver jdbc.connection.url=jdbc\:oracle\:thin\:@localhost\:1521\:ORCL jdbc.connection.username=myhr jdbc.connection.password=myhr
然后看读取数据库配置文件的工具类。
import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ConfigManager { private static ConfigManager configManager;//声明ConfigManager对象 private static Properties properties;//声明Properties对象 private ConfigManager(){ String configPath = "database.properties"; properties = new Properties(); InputStream in = ConfigManager.class.getResourceAsStream(configPath); try { properties.load(in); } catch (IOException e) { e.printStackTrace(); } } private static ConfigManager getInstance(){ if(configManager == null){ configManager = new ConfigManager(); } return configManager; } public String getString(String key){ return properties.getProperty(key); } }
然后看数据库操作代码,看JDBC如何具体操作数据库
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; import java.util.Date; public class NewsDao { Connection connection = null; Statement statement = null; PreparedStatement pstmt = null; ResultSet rs = null; //建立与数据库之间的连接 public void getConnection(){ //调用工具类的相应方法,得到配置文件中的信息 String driver = ConfigManager.getInstance().getString("jdbc.driver_class"); String url = ConfigManager.getInstance().getString("jdbc.connection.url"); String username = ConfigManager.getInstance().getString("jdbc.connection.username"); String password = ConfigManager.getInstance().getString("jdbc.connection.password"); try { //使用Class.froName方法加载驱动类 Class.forName(driver); //DriverManager类的getConnection方法建立于数据库连接 connection = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } //查询新闻 public void getNewsList(){ getConnection(); String sql = "select * from news"; try { //获得Statement对象 statement = connection.createStatement(); //执行SQL语句并得到ResultSet rs = statement.executeQuery(sql); //处理执行结果 while(rs.next()){ int id = rs.getInt("id"); String title = rs.getString("title"); String summary = rs.getString("summary"); String content = rs.getString("content"); String author = rs.getString("author"); Timestamp time = rs.getTimestamp("createdate"); Timestamp last = rs.getTimestamp("MIDIFYDATE"); System.out.println("新闻id:"+id+" 标题:"+title+" 摘要:"+summary +" 内容:"+content+" 作者:"+author+" 创建时间:"+time+" 修改时间:"+last); } } catch (SQLException e) { e.printStackTrace(); }finally{ try { rs.close(); statement.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } //添加新闻信息 public void add(int id,String title,String summary,String content, String author,Date createdate,Date last){ getConnection(); String sql = "insert into news values(?,?,?,?,?,?,?)"; try { pstmt = connection.prepareStatement(sql); pstmt.setInt(1, id); pstmt.setString(2, title); pstmt.setString(3, summary); pstmt.setString(4, content); pstmt.setString(5, author); pstmt.setTimestamp(6, new java.sql.Timestamp(createdate.getTime())); pstmt.setTimestamp(7, new java.sql.Timestamp(last.getTime())); int i = pstmt.executeUpdate();//i表示插入的行数 if(i>0) System.out.println("插入成功!"); else System.out.println("插入失败!"); } catch (SQLException e) { e.printStackTrace(); }finally{ try { pstmt.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
然后看测试类
import java.util.Date; public class Test { public static void main(String[] args) { NewsDao nd = new NewsDao(); nd.getNewsList(); nd.add(2, "有蚊子", "今天竟然有蚊子", "今天竟然真的有蚊子,咬了我好几口!", "admin", new Date(), new Date()); } }
程序正常运行。

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

toadduserInmysqleffectection andsecrely,theTheSepsps:1)USEtheCreateuserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNectalRevileSaryPrivilegesSustate,usiveleanttatement,AdheringTotheTeprinciplelastPrevilegege.3)

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)创建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。3)GrantWriteAccessto'

MySQL中的字符串数据类型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序规则(Collations)决定了字符串的比较和排序方式。1.CHAR适合固定长度字符串,VARCHAR适合可变长度字符串。2.BINARY和VARBINARY用于二进制数据,BLOB和TEXT用于大对象数据。3.排序规则如utf8mb4_unicode_ci忽略大小写,适合用户名;utf8mb4_bin区分大小写,适合需要精确比较的字段。

最佳的MySQLVARCHAR列长度选择应基于数据分析、考虑未来增长、评估性能影响及字符集需求。1)分析数据以确定典型长度;2)预留未来扩展空间;3)注意大长度对性能的影响;4)考虑字符集对存储的影响。通过这些步骤,可以优化数据库的效率和扩展性。

mysqlblobshavelimits:tinyblob(255bytes),blob(65,535 bytes),中间布洛布(16,777,215个比例),andlongblob(4,294,967,967,295 bytes).tousebl观察性:1)考虑performance impactsandSandStorelargeblobsextern; 2)管理backbackupsandreplication carecration; 3)usepathsinst

自动化在MySQL中创建用户的最佳工具和技术包括:1.MySQLWorkbench,适用于小型到中型环境,易于使用但资源消耗大;2.Ansible,适用于多服务器环境,简单但学习曲线陡峭;3.自定义Python脚本,灵活但需确保脚本安全性;4.Puppet和Chef,适用于大规模环境,复杂但可扩展。选择时需考虑规模、学习曲线和集成需求。

是的,YouCansearchInIdeAblobInMysqlusingsPecificteChniques.1)转换theblobtoautf-8StringWithConvertFunctionWithConvertFunctionandSearchusiseLike.2)forCompresseBlyblobs,useuncompresseblobs,useuncompressbeforeconversion.3)acpperformance impperformance imperformance imptactsanddataEccoding.4)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

Atom编辑器mac版下载
最流行的的开源编辑器

记事本++7.3.1
好用且免费的代码编辑器

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。