利用JDBC实现对mysql数据库的插,查,删,改
2007-11-08 19:59
363 查看
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.bean.NoticeBean;
public class JDBCTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
String driverName="com.mysql.jdbc.Driver";
Class.forName(driverName);
String url="jdbc:mysql://localhost:3306/java?useUnicode=true&characterEncoding=gb2312";
conn=DriverManager.getConnection(url,"root","root");
System.out.println("连接MySql成功!!!");
stmt=null;
rs=null;
String strSql=null;
NoticeBean bean=null;
String title=null;
String content=null;
try {
title="标题";
content="内容";
strSql="INSERT INTO notice(title,content) VALUES('"+title+"','"+content+"')";
stmt=conn.createStatement();
stmt.executeUpdate(strSql);
System.out.println("插入语句执行成功:"+strSql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("插入失败");
}
strSql="select * from notice";
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(strSql);
if(rs.next()){
int id=rs.getInt("id");
title =rs.getString("title");
content=rs.getString("content");
if(rs.next()){
bean=new NoticeBean(id,title,content);
}
System.out.println("notice第一行数据是"+bean.getId()+" "+bean.getTitle()+" "+bean.getContent());
}
try {
strSql="delete from notice";
stmt=conn.createStatement();
stmt.executeUpdate(strSql);
System.out.println("删除完成");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("删除失败");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.bean.NoticeBean;
public class JDBCTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
String driverName="com.mysql.jdbc.Driver";
Class.forName(driverName);
String url="jdbc:mysql://localhost:3306/java?useUnicode=true&characterEncoding=gb2312";
conn=DriverManager.getConnection(url,"root","root");
System.out.println("连接MySql成功!!!");
stmt=null;
rs=null;
String strSql=null;
NoticeBean bean=null;
String title=null;
String content=null;
try {
title="标题";
content="内容";
strSql="INSERT INTO notice(title,content) VALUES('"+title+"','"+content+"')";
stmt=conn.createStatement();
stmt.executeUpdate(strSql);
System.out.println("插入语句执行成功:"+strSql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("插入失败");
}
strSql="select * from notice";
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(strSql);
if(rs.next()){
int id=rs.getInt("id");
title =rs.getString("title");
content=rs.getString("content");
if(rs.next()){
bean=new NoticeBean(id,title,content);
}
System.out.println("notice第一行数据是"+bean.getId()+" "+bean.getTitle()+" "+bean.getContent());
}
try {
strSql="delete from notice";
stmt=conn.createStatement();
stmt.executeUpdate(strSql);
System.out.println("删除完成");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("删除失败");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
相关文章推荐
- java利用jdbc连接Mysql数据库——实现登录注册功能
- 利用JDBC实现java与MySQL数据库的连接及相关操作
- JAVA 利用JDBC连接MYSQL数据库以及增删改查的实现【学习笔记】
- 利用Sharding-JDBC实现分表
- 利用Sharding-JDBC实现分表
- 利用Sharding-JDBC实现分表
- 利用SQLyog实现mysql数据库定时自动备份
- 利用Sharding-Jdbc实现分表
- 利用Navicat工具实现MySQL数据库结构同步
- sqlite_test自己利用jdbc创建一个关系型数据库 实现增删改查
- JDBC:DAO(三)利用queryRunner实现一个较好的DAO架构
- 利用JDBC连接MySql数据库
- 利用Sharding-JDBC实现分表
- 利用Sharding-JDBC实现分表
- 利用Sharding-JDBC实现分表
- mybatis连接mysql数据库实现的jdbc功能
- 利用Java针对MySql封装的jdbc框架类 JdbcUtils 完整实现(包含增删改查、JavaBean反射原理,附源码)
- 如何实现JDBC连接mysql数据库
- PowerDesign利用jdbc驱动实现逆向工程
- 利用EXCEL表实现网页数据采集到MYSQL数据库