Rhythmk 一步一步学 JAVA(2) : 操作 MYSQL 数据库
2013-01-26 17:26
417 查看
1. 下载 如:mysql-connector-java-5.1.22.zip 解压获取 jar 连接包。
2. 引入项目: 右键项目名--->Build Path—>Configure Build Path... 选择 Libraries 然后再选择 右边
第二个选项Add External Jars 导入 mysql -.jar
3. 新建数据表:
4. 测试程序:
2. 引入项目: 右键项目名--->Build Path—>Configure Build Path... 选择 Libraries 然后再选择 右边
第二个选项Add External Jars 导入 mysql -.jar
3. 新建数据表:
CREATE TABLE `user` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(32) NOT NULL, `Age` int(11) NOT NULL, `AddTime` datetime NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
4. 测试程序:
package App1.Rhythmk.com; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import com.mysql.jdbc.Statement; /* * * 建表脚本 CREATE TABLE `user` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(32) NOT NULL, `Age` int(11) NOT NULL, `AddTime` datetime NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8; */ public class ThirdMysql { // final 类似 .NET 中 const private static final String DatabaseConURL = "jdbc:mysql://127.0.0.1:3306/rhythmkdb"; /** * rhythmK * 1.右键项目名--->Build Path—>Configure Build Path... 会弹出来一个框 在那四个选项选择 * Libraries 然后再选择 右边 第二个选项Add External Jars */ public static void main(String[] args) { // TODO Auto-generated method stub Test(); ExcuteByParam(); Add(); Update(); Select(); Del(); } public static void Test() { try { Connection con = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager .getConnection(DatabaseConURL, "root", "wangkun"); Msg("Mysql 连接成功。"); } catch (Exception ex) { Msg(ex.toString()); } } public static void Add() { String sql = "Insert into `user` (Name ,Age, Addtime ) "; sql += " values ('rhythmk',21,'2012-1-1') "; try { Connection con = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager .getConnection(DatabaseConURL, "root", "wangkun"); java.sql.Statement statement = con.createStatement(); statement.executeUpdate(sql); ResultSet result = statement .executeQuery("select LAST_INSERT_ID() "); int resultID = 0; if (result.next()) { // 此出索引不是从0开始 .. resultID = result.getInt(1); } Msg("Add 数据库得到返回ID" + resultID); } catch (Exception ex) { Msg(ex.toString()); } } public static void Update() { String sql = " Update `user` set Name= 'rhythmk.com' where ID<3 "; try { Connection con = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager .getConnection(DatabaseConURL, "root", "wangkun"); java.sql.Statement statement = con.createStatement(); int resultID = statement.executeUpdate(sql); Msg("Update 数据库得到返回ID" + resultID); } catch (Exception ex) { Msg(ex.toString()); } } public static void Del() { String sql = " Delete from `user` where ID=3 "; try { Connection con = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager .getConnection(DatabaseConURL, "root", "wangkun"); java.sql.Statement statement = con.createStatement(); int resultID = statement.executeUpdate(sql); Msg("Del 数据库得到返回ID" + resultID); } catch (Exception ex) { Msg(ex.toString()); } } public static void Select() { String sql = " Select ID,Name from `user` "; try { Connection con = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager .getConnection(DatabaseConURL, "root", "wangkun"); java.sql.Statement statement = con.createStatement(); ResultSet result = statement.executeQuery(sql); String resultStr = ""; while (result.next()) { resultStr += "\r\n ID=" + result.getString(1); resultStr += ", Name=" + result.getString("Name"); } Msg(resultStr); // statement.executeUpdate(sql); } catch (Exception ex) { Msg(ex.toString()); } } public static void ExcuteByParam() { String sql = "select * from `user` where Name =?"; try { Connection con = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager .getConnection(DatabaseConURL, "root", "wangkun"); java.sql.PreparedStatement preparedsm = con.prepareStatement(sql); preparedsm.setString(1, "rhythmk"); ResultSet result = preparedsm.executeQuery(); String resultStr = ""; while (result.next()) { resultStr += "\r\n ID=" + result.getString(1); resultStr += ", Name=" + result.getString("Name"); } Msg("ExcuteByParam:\r\n" + resultStr); // statement.executeUpdate(sql); } catch (Exception ex) { Msg(ex.toString()); } } public static void Msg(String msg) { System.out.println(msg); } }
相关文章推荐
- Java 使用 DBCP mysql 连接池 做数据库操作
- java 操作mysql的创建数据库及数据的增删改查
- java连接mysql实例及数据库操作
- Java数据库操作(Mysql连接,显示,添加,删除)
- Java学习笔记——JDBC之与数据库MySQL的连接以及增删改查等操作
- JAVA操作mysql(如何更加面向对象的操作数据库)
- 一步一步学MySQL----2 数据库基本操作
- mySql事务_ _Java中怎样实现批量删除操作(Java对数据库进行事务处理)?
- JAVA 数据库操作工具类(项目封装,mySQL)
- java 操作mysql的创建数据库及数据的增删改查
- Java语句轻松实现与数据库MYSQL【本地数据库】的连接,和对数据库的增删改查操作
- 【MySQL 05】使用Java对MySQL进行操作(创建数据库)
- Java 连接到 MySQL 数据库的代码及相关操作
- java 数据库操作(mysql)
- 编写操作数据库的JAVA程序时需要的连接MySQL数据库的JDBC连接包mysql-connector-java-5.1.10.zip怎么安装
- 转载:Java连接MySQL 数据库的正确操作流程
- java对三大主流数据库sql server、mysql、oracle的连接和操作
- java操作数据库MYSQL
- Java连接MySQL 数据库的正确操作流程
- 基于JDBC的java数据库操作框架DB-helper(目前仅支持mysql)