一、加载驱动
Class.forName("com.mysql.jdbc.Driver");
二、准备连接字段
String url = "jdbc:mysql://localhost3306:数据库名“;
String user = "root";
String pwd = ".....";
三、获取连接对象
Connection con = DriverManager.getConnection(url,user,pwd);
四、编写sql语句
String sql = "select * from 表名”
五、获取处理对象
PreStatement pre = con.creatPreStatement();
六、处理结果
查询:ResultSet re = pre.excuteQuerry();
输出:while(re.next){
Sysout.out.pritln(re.getString("name")+re.getInt("ID")
}
增删改:int count = pre.excuteUpdate();
七、关闭资源
re.close();
pre.close();
con.close();
本文详细介绍了如何使用Java编程语言连接并操作MySQL数据库,包括加载驱动、设置连接参数、建立连接、执行SQL语句(查询、增删改)及资源关闭等关键步骤,是Java数据库连接的基础教程。
3472

被折叠的 条评论
为什么被折叠?



