MyEclipse通过JDBC连接MySQL数据库基本介绍_MySQL
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.
