急 用java写一个 连接数据库 增删改的例子
6个回答
展开全部
DBConnectionManager.java //连接数据库用的
import java.sql.*;
public class DBConnectionManager {
private String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo";
private String user="sa";
private String password="";
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Connection getConnection(){
try{
Class.forName(driverName);
return DriverManager.getConnection(url, user, password);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
DBSQLManager.java //操作数据库用的
import java.sql.*;
public class DBSQLManager {
protected Connection con=null;//Connection对象
protected Statement stmt=null;//Statement对象
protected ResultSet rs=null;//记录结果集
protected String sql=""; //SQL语句
public DBSQLManager(){
try {
DBConnectionManager dcm=new DBConnectionManager();
con=dcm.getConnection();
//con.setAutoCommit(false);//添加事物,既是否自动提交
stmt=con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Statement getStmt(){
return stmt;
}
public Connection getCon(){
return con;
}
public ResultSet getRs(){
return rs;
}
public void setSql(String sql){
this.sql=sql;
}
public String getSql(){
return sql;
}
//查找
public void execueQuery(){
try {
rs=stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
//更新
public void executeUpdate(){
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭
public void close(){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs=null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt=null;
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con=null;
}
}
SqlOperate.java //用来调用数据库操作语句
import java.sql.ResultSet;
import java.sql.SQLException;
public class SqlOperate {
//插入,修改,删除
public void insOrModOrDel(String sql){
DBSQLManager dbsm=new DBSQLManager();
dbsm.getStmt();
dbsm.setSql(sql);
dbsm.executeUpdate();
dbsm.close();
}
//显示
public void display(String sql){
DBSQLManager dbsm=new DBSQLManager();
dbsm.getStmt();
dbsm.setSql(sql);
dbsm.execueQuery();
ResultSet rs=dbsm.getRs();
try {
while(rs!=null&&rs.next()){
System.out.print(rs.getObject(1)+"\t");
System.out.print(rs.getObject(2)+"\t");
System.out.print(rs.getObject(3)+"\t");
System.out.println(rs.getObject(4));
}
dbsm.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
SqlMain.java //数据库的测试主函数
public class SqlMain {
public static void main(String[] args){
SqlOperate sqlOpt=new SqlOperate();
// sqlOpt.insOrModOrDel("insert into user1 values('qianhaifei',999999,'qianhaifei@163.com')");//插入
// sqlOpt.insOrModOrDel("update user1 set username='weixiangyang' where id=4");//修改
// sqlOpt.insOrModOrDel("delete from user1 where username='weixy2000'");//删除
sqlOpt.display("select *from user1");//显示
}
}
楼主用的时间只需要将数据库 用户名和密码还有数据库表名改一下就行了...
其他都是一样的...
楼主会改吧.../???
如果有什么问题的话请百度HI我...帮你解决....
如果楼主要我的数据库的话....请百度HI我...我给你就行了...
祝楼主早日成功!
import java.sql.*;
public class DBConnectionManager {
private String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo";
private String user="sa";
private String password="";
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Connection getConnection(){
try{
Class.forName(driverName);
return DriverManager.getConnection(url, user, password);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
DBSQLManager.java //操作数据库用的
import java.sql.*;
public class DBSQLManager {
protected Connection con=null;//Connection对象
protected Statement stmt=null;//Statement对象
protected ResultSet rs=null;//记录结果集
protected String sql=""; //SQL语句
public DBSQLManager(){
try {
DBConnectionManager dcm=new DBConnectionManager();
con=dcm.getConnection();
//con.setAutoCommit(false);//添加事物,既是否自动提交
stmt=con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Statement getStmt(){
return stmt;
}
public Connection getCon(){
return con;
}
public ResultSet getRs(){
return rs;
}
public void setSql(String sql){
this.sql=sql;
}
public String getSql(){
return sql;
}
//查找
public void execueQuery(){
try {
rs=stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
//更新
public void executeUpdate(){
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭
public void close(){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs=null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt=null;
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con=null;
}
}
SqlOperate.java //用来调用数据库操作语句
import java.sql.ResultSet;
import java.sql.SQLException;
public class SqlOperate {
//插入,修改,删除
public void insOrModOrDel(String sql){
DBSQLManager dbsm=new DBSQLManager();
dbsm.getStmt();
dbsm.setSql(sql);
dbsm.executeUpdate();
dbsm.close();
}
//显示
public void display(String sql){
DBSQLManager dbsm=new DBSQLManager();
dbsm.getStmt();
dbsm.setSql(sql);
dbsm.execueQuery();
ResultSet rs=dbsm.getRs();
try {
while(rs!=null&&rs.next()){
System.out.print(rs.getObject(1)+"\t");
System.out.print(rs.getObject(2)+"\t");
System.out.print(rs.getObject(3)+"\t");
System.out.println(rs.getObject(4));
}
dbsm.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
SqlMain.java //数据库的测试主函数
public class SqlMain {
public static void main(String[] args){
SqlOperate sqlOpt=new SqlOperate();
// sqlOpt.insOrModOrDel("insert into user1 values('qianhaifei',999999,'qianhaifei@163.com')");//插入
// sqlOpt.insOrModOrDel("update user1 set username='weixiangyang' where id=4");//修改
// sqlOpt.insOrModOrDel("delete from user1 where username='weixy2000'");//删除
sqlOpt.display("select *from user1");//显示
}
}
楼主用的时间只需要将数据库 用户名和密码还有数据库表名改一下就行了...
其他都是一样的...
楼主会改吧.../???
如果有什么问题的话请百度HI我...帮你解决....
如果楼主要我的数据库的话....请百度HI我...我给你就行了...
祝楼主早日成功!
展开全部
//定义连接数据库所需的驱动
String DBDRIVER="org.git.mm.mysql.driver";
String DBURL="jdbc:mysql://localhost:3306/ssh?user=root&password=root&useUnicode=true&charcterEncoding=gbk";
//ssh 是你的数据库名,
//连接变量
Connection conn=null;
//准备声明
PreparedStatement pstmt=null;
//设置结果集变量
ResultSet rs=null;
String sql="selcet name from ssh.person where id=? and password=?";
try{
Class.forName(DBDRIVER);
//获取数据库连接
conn=DriverManager.getConnection(DBURL);
//导入sql语句,进行预编译
pstmt=conn.prepareStatement(sql);
大概的数据库的连接和对数据库的操作都给你了,自己试试吧!
String DBDRIVER="org.git.mm.mysql.driver";
String DBURL="jdbc:mysql://localhost:3306/ssh?user=root&password=root&useUnicode=true&charcterEncoding=gbk";
//ssh 是你的数据库名,
//连接变量
Connection conn=null;
//准备声明
PreparedStatement pstmt=null;
//设置结果集变量
ResultSet rs=null;
String sql="selcet name from ssh.person where id=? and password=?";
try{
Class.forName(DBDRIVER);
//获取数据库连接
conn=DriverManager.getConnection(DBURL);
//导入sql语句,进行预编译
pstmt=conn.prepareStatement(sql);
大概的数据库的连接和对数据库的操作都给你了,自己试试吧!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
JDBC最基本的增、删、改、查(注意代码的规范和严谨)
public class JDBCTest {
public static void main(String[] args) {
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/ssh?
conn=DriverManager.getConnection(url,"root","root");
System.out.println("连接MySql成功!!!");
stmt=null;
rs=null;
String strSql=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) {
e.printStackTrace();
System.out.println("插入失败");
}
strSql="select * from notice";
stmt=conn.createStatement();
rs=stmt.executeQuery(strSql);
if(rs.next()){
int id=rs.getInt("id");
title =rs.getString("title");
content=rs.getString("content");
}
try {
strSql="delete from notice";
stmt=conn.createStatement();
stmt.executeUpdate(strSql);
System.out.println("删除完成");
} catch (RuntimeException e) {
e.printStackTrace();
System.out.println("删除失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
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) {
e.printStackTrace();
}
}
}
}
public class JDBCTest {
public static void main(String[] args) {
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/ssh?
conn=DriverManager.getConnection(url,"root","root");
System.out.println("连接MySql成功!!!");
stmt=null;
rs=null;
String strSql=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) {
e.printStackTrace();
System.out.println("插入失败");
}
strSql="select * from notice";
stmt=conn.createStatement();
rs=stmt.executeQuery(strSql);
if(rs.next()){
int id=rs.getInt("id");
title =rs.getString("title");
content=rs.getString("content");
}
try {
strSql="delete from notice";
stmt=conn.createStatement();
stmt.executeUpdate(strSql);
System.out.println("删除完成");
} catch (RuntimeException e) {
e.printStackTrace();
System.out.println("删除失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
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) {
e.printStackTrace();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.util.Properties;
public class jdbcData {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Properties info=new Properties();
info.setProperty("user", "用户名");
info.setProperty("password", "密码");
String driverClass="com.mysql.jdbc.Driver";
String driverUrl="jdbc:mysql://localhost:3306/实例名";
Connection conn=DriverManager.getConnection(url,info);
Statement st=conn.createStatement();
st.executeQuery("insert into ....")//增
st.executeQuery("delete .....from....")//删
st.executeQuery("update.....")//改
ResultSet rs=st.executeQuery("select * from customers order by id");
System.out.println("客户ID 姓名 年龄");
while(rs.next()){
int id =rs.getInt(1);
String name=rs.getString("name");
String age=rs.getString("age");
System.out.println(id+" "+name+" "+age);
}
System.out.println();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.util.Properties;
public class jdbcData {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Properties info=new Properties();
info.setProperty("user", "用户名");
info.setProperty("password", "密码");
String driverClass="com.mysql.jdbc.Driver";
String driverUrl="jdbc:mysql://localhost:3306/实例名";
Connection conn=DriverManager.getConnection(url,info);
Statement st=conn.createStatement();
st.executeQuery("insert into ....")//增
st.executeQuery("delete .....from....")//删
st.executeQuery("update.....")//改
ResultSet rs=st.executeQuery("select * from customers order by id");
System.out.println("客户ID 姓名 年龄");
while(rs.next()){
int id =rs.getInt(1);
String name=rs.getString("name");
String age=rs.getString("age");
System.out.println(id+" "+name+" "+age);
}
System.out.println();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我有这样的类 特别实用
或许可以帮到你!!
或许还可以经常交流下 相关的技术方面问题
如果 在,发消息我
或许可以帮到你!!
或许还可以经常交流下 相关的技术方面问题
如果 在,发消息我
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
晚上有时间给你写
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询