Java连接mysql数据库代码实例程序
作者:沃德吉尔·邦英 时间:2024-01-22 03:01:26
这篇文章主要介绍了java连接mysql数据库代码实例程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
用java 联接mysql的实例
在联接的时候,先确保本机安装了mysql或者服务器是安装了mysql
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MainSql {
// JDBC 驱动名及数据库 URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/chat";
// 数据库的用户名与密码,需要根据自己的设置
static final String USER = "root";
static final String PASS = "";
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null;
Statement stmt = null;
try{
Class.forName(JDBC_DRIVER);
System.out.println("链接数据库..");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql = "SELECT * FROM users";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
// 通过字段检索
String id = rs.getString("id");
String password = rs.getString("passwd");
// 输出数据
System.out.println("用户名: " + id);
System.out.println("密码: " + password);
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
// 处理 JDBC 错误
se.printStackTrace();
}catch(Exception e){
// 处理 Class.forName 错误
e.printStackTrace();
}finally{
// 关闭资源
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}// 什么都不做
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
这就是mysql联接的实例程序,这里我就只粘贴了java调用mysql,bingqie建立联接的代码,并没有调用到sql语句,其他部分可以在网上找到
来源:https://www.cnblogs.com/loveshit/p/11923071.html
标签:java,连接,mysql,数据库
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
快速解决 MySQL中与浮点比较有关的问题
2008-11-27 16:28:00
浅谈javascript面向对象编程
2009-03-03 12:04:00
用Python实现简单的人脸识别功能步骤详解
2022-05-15 15:52:00
![](https://img.aspxhome.com/file/2023/8/103788_0s.jpg)
Pytorch 如何加速Dataloader提升数据读取速度
2023-11-12 00:00:32
![](https://img.aspxhome.com/file/2023/6/115196_0s.jpg)
Python实现图像的垂直投影示例
2023-12-06 13:20:15
![](https://img.aspxhome.com/file/2023/1/76791_0s.jpg)
Python编写memcached启动脚本代码实例
2023-02-13 19:59:51
Python OpenCV学习之图像形态学
2022-01-16 21:48:29
![](https://img.aspxhome.com/file/2023/6/93746_0s.jpg)
基于python SMTP实现自动发送邮件教程解析
2023-03-08 20:15:02
![](https://img.aspxhome.com/file/2023/2/121602_0s.png)
python实现根据主机名字获得所有ip地址的方法
2021-03-13 14:19:25
Python实现计算文件MD5和SHA1的方法示例
2023-12-07 06:55:46
SQL查询语句优化的实用方法总结
2024-01-25 18:51:21
asp如何实时获知多少人在线?
2010-05-18 18:34:00
python制作填词游戏步骤详解
2023-03-09 19:46:47
![](https://img.aspxhome.com/file/2023/5/89365_0s.png)
Go语言流程控制详情
2023-10-16 13:16:24
Javascript正则表达式基础
2009-02-01 18:13:00
MySQL系列多表连接查询92及99语法示例详解教程
2024-01-21 03:05:33
![](https://img.aspxhome.com/file/2023/9/115559_0s.png)
切换路径在Jupyter里调用本地文件的操作
2022-05-24 15:03:54
![](https://img.aspxhome.com/file/2023/0/66350_0s.jpg)
sqlserver中在指定数据库的所有表的所有列中搜索给定的值
2011-09-30 11:27:38
Python实现复杂对象转JSON的方法示例
2021-11-14 09:55:47
mysql优化的重要参数 key_buffer_size table_cache
2024-01-16 03:01:52
![](https://img.aspxhome.com/file/2023/5/119735_0s.jpg)