java实现mysql操作类分享java连接mysql
相关软件和驱动:
Mysql下载版本:4.1.11
http://dev.mysql.com/downloads/mysql/4.1.html
JDBC驱动下载版本:3.1.8
http://dev.mysql.com/downloads/connector/j/3.1.html
代码
CodehighlightingproducedbyActiproCodeHighlighter(freeware)http://www.CodeHighlighter.com/-->importjava.sql.*;
publicclassmysql{
publicstaticStringurl="jdbc:mysql://localhost:3306/test";//characterEncoding=GBK
publicstaticStringusername="root";
publicstaticStringpassword="root";
publicstaticConnectioncon;
publicstaticStatementstmt;
publicstaticResultSetrs;
publicstaticvoidmain(String[]args)throwsSQLException{
connect();
operation();
stmt.close();
con.close();
}
publicstaticvoidtest(){
Stringsql_select="select*fromtablenamewhereid=1";
Stringsql_insert="insertintotablename(col1,col2..)values("1","2"...)";
Stringsql_update="updatetablenamesetcolname="update"whereid=1";
//insert(sql_insert);
//select(sql_select);
//update(sql_update);
}
publicstaticvoidconnect(){
//定位驱动
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载驱动成功!");
}catch(ClassNotFoundExceptione){
System.out.println("加载驱动失败!");
e.printStackTrace();
}
//建立连接
try{
con=DriverManager.getConnection(url,username,password);
stmt=con.createStatement();
System.out.println("数据库连接成功!");
}catch(SQLExceptione){
System.out.println("数据库连接失败!");
}
}
publicstaticvoidselect(Stringsql){
try{
rs=stmt.executeQuery(sql);
ResultSetMetaDatameta_data=rs.getMetaData();//列名
for(inti_col=1;i_col<=meta_data.getColumnCount();i_col++){
System.out.print(meta_data.getColumnLabel(i_col)+" ");
}
System.out.println();
while(rs.next()){
for(inti_col=1;i_col<=meta_data.getColumnCount();i_col++){
System.out.print(rs.getString(i_col)+" ");
}
System.out.println();
}
rs.close();
}catch(Exceptione){
System.out.println("数据查询失败!");
}
}
publicstaticvoidinsert(Stringsql){
try{
stmt.clearBatch();
stmt.addBatch(sql);
stmt.executeBatch();
System.out.println("数据插入成功!");
}catch(Exceptione){
System.out.println("数据插入失败!");
}
}
publicstaticvoidupdate(Stringsql){
try{
stmt.executeUpdate(sql);
System.out.println("数据更新成功!");
}catch(Exceptione){
System.out.println("数据更新失败!");
}
}
}
相关文章
- Java使用JDBC连接MYSQL数据库增删改查示例详解编程语言
- 实现Myeclipse快捷连接Mysql数据库(myeclipse连接mysql数据库)
- MySQL Status Mysqlx_stmt_ping 数据库状态作用意思及如何正确
- 数据MySQL提取前10条数据的实现方式(mysql取10条)
- MySQL: 开启远程连接之旅(mysql允许远程连接)
- 远程连接让MySQL远程连接:简单三步实现(设置mysql)
- MySQL加速器:最大限度地提升性能(mysql加速器)
- Oracle与Java的连接:揭秘数据库开发技术之间的神奇联系(oracle连接java)
- 配置PHP实现MySQL数据库连接(php连接mysql配置)
- MySQL编码修改:从未如此简单(mysql编码修改)
- MySQL端口:数据库连接的关键(mysql端口)
- MySQL远程连接:操作指南及用户管理(mysql远程连接用户)
- 的安装安装MySQL客户端,实现安卓端数据库连接(安卓端mysql客户端)
- Ajax技术实现MySQL数据查询与更新(ajax连接mysql)
- C语言实现MySQL数据库连接的简单方法(c mysql连接语句)
- MySQL连接服务器C语言实现(c mysql连接方法)
- 比较MySQL与Oracle的优势之处(mysql或oracle)
- MySQL在55版本的安装指南(5.5mysql安装)
- 妙用命令行清理MySQL空间(cmd mysql 清屏)
- MySQL连接检查的使用方法介绍(mysql中关于连接检查)
- MySQL三表查询左连接技巧详解(mysql三表查询左连接)
- MySQL连接时效限制与优化(mysql一般连接多久)
- MySQL是或否(mysql yes no)
- MySQL的YEAR函数提取日期中的年份(mysql year函数)
- MySQL实现两层左关联操作(mysql两层左关联)