bitsCN.com
1.前提是MyEclipse已经能正常开发Java工程
2.安装MySQL
个人使用的是版本是 mysql-5.0.22-win32.zip
网址:http://www.mysql.com/downloads/mysql/#downloads
3.下载JDBC驱动
个人使用的是 mysql-connector-java-5.1.22.zip,所需要的就是解压缩之后其中的 mysql-connector-java-5.1.22-bin.jar
网址:http://www.mysql.com/downloads/connector/j/
4.代码测试
package ts.jsj.lyh;
import java.sql.*;
/** *//**
* 使用JDBC连接数据库MySQL的过程
* DataBase:JSJ, table:student;
* @author DuChangfeng 2008 09 18
*/
public class JDBCTest {
public static Connection getConnection() throws SQLException,
java.lang.ClassNotFoundException
{
//第一步:加载MySQL的JDBC的驱动
Class.forName("com.mysql.jdbc.Driver");
//取得连接的url,能访问MySQL数据库的用户名,密码;jsj:数据库名
String url = "jdbc:mysql://localhost:3306/jsj";
String username = "root";
String password = "111";
//第二步:创建与MySQL数据库的连接类的实例
Connection con = DriverManager.getConnection(url, username, password);
return con;
}
public static void main(String args[]) {
try
{
//第三步:获取连接类实例con,用con创建Statement对象类实例 sql_statement
Connection con = getConnection();
Statement sql_statement = con.createStatement();
/** *//************ 对数据库进行相关操作 ************/
//如果同名数据库存在,删除
//sql_statement.executeUpdate("drop table if exists student");
//执行了一个sql语句生成了一个名为student的表
//sql_statement.executeUpdate("create table student (id int not null auto_increment, name varchar(20) not null default 'name', math int not null default 60, primary key (id) ); ");
//向表中插入数据
//sql_statement.executeUpdate("insert student values(1, 'liying', 98)");
//sql_statement.executeUpdate("insert student values(2, 'jiangshan', 88)");
//sql_statement.executeUpdate("insert student values(3, 'wangjiawu', 78)");
//sql_statement.executeUpdate("insert student values(4, 'duchangfeng', 100)");
//---以上操作不实用,但是列出来作为参考---
//第四步:执行查询,用ResultSet类的对象,返回查询的结果
String query = "select * from student";
ResultSet result = sql_statement.executeQuery(query);
/** *//************ 对数据库进行相关操作 ************/
System.out.println("Student表中的数据如下:");
System.out.println("------------------------");
System.out.println("学号" + " " + "姓名" + " " + "数据成绩 ");
System.out.println("------------------------");
//对获得的查询结果进行处理,对Result类的对象进行操作
while (result.next())
{
int number = result.getInt("sno");
String name = result.getString("sname");
String mathScore = result.getString("sgrade");
//取得数据库中的数据
System.out.println(" " + number + " " + name + " " + mathScore);
}
//关闭连接和声明
sql_statement.close();
con.close();
} catch(java.lang.ClassNotFoundException e) {
//加载JDBC错误,所要用的驱动没有找到
System.err.print("ClassNotFoundException");
//其他错误
System.err.println(e.getMessage());
} catch (SQLException ex) {
//显示数据库连接错误或查询错误
System.err.println("SQLException: " + ex.getMessage());
}
}
}
以上大部分内容整理自网络,感谢猿猿们的无私奉献~~具体的步骤、强大的互联网上都比较容易查询的到,这里不再赘述,现加上几点个人认为需要注意的地方:
1)关于mysql-connector-java-5.1.22-bin.jar 的存放位置。在MyEclipse具体的java工程中新建一存放jar 包的文件夹(如 lib),将mysql-connector-java-5.1.22-bin.jar 复制到文件夹中,选中jar包右击--->Build Path--->Add To Build Path,即可。
若出现
ClassNotFoundExceptioncom.mysql.jdbc.Driver
的提示,则正是由于缺少导入jar包所造成的。
2)如果已经对MySQL的使用很熟悉,则可忽略这条。个人在测试连接时,老是出现这样的异常提示:
SQLException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
这正是由于个人对MySQL使用不熟悉,对MySQL进行了诸多尝试性的操作,不知何时无意中将MySQL的服务(如果在安装MySQL时没有更改的话,缺省服务名就是MySQL)关闭,解决方法开启此服务即可。控制面板--->管理工具--->服务--->MySQL--->选择启用。
3)在使用上面的代码测试时,需要更改的地方有:
//MySQL数据库的用户名,密码,数据库名
String url = "jdbc:mysql://localhost:3306/jsj";
String username = "root";
String password = "111";
以及具体基本表中的所要查询的字段名:
int number = result.getInt("sno");
String name = result.getString("sname");
String mathScore = result.getString("sgrade");
多多分享,有问题欢迎交流~~bitsCN.com

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

通过PHP网页界面添加MySQL用户可以使用MySQLi扩展。步骤如下:1.连接MySQL数据库,使用MySQLi扩展。2.创建用户,使用CREATEUSER语句,并使用PASSWORD()函数加密密码。3.防止SQL注入,使用mysqli_real_escape_string()函数处理用户输入。4.为新用户分配权限,使用GRANT语句。

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollationsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

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

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

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