如何写一个dao类方法来实现java对mysql数据库的增删改查?

2024年12月04日 23:31
有2个网友回答
网友(1):

package basic;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBC {

public void findAll() {

try {

// 获得数据库驱动

//由于长时间不写,驱动名和URL都忘记了,不知道对不对,你应该知道的,自己改一下的哈

String url = "jdbc:oracle:thin:@localhost:1521:XE";

String userName = "system";

String password = "system";

Class.forName("oracle.jdbc.driver.OracleDriver");

// 创建连接

Connection conn = DriverManager.getConnection(url, userName,
password);

// 新建发送sql语句的对象

Statement st = conn.createStatement();

// 执行sql

String sql = "select * from users";

ResultSet rs = st.executeQuery(sql);

// 处理结果

while(rs.next()){

//这个地方就是给你的封装类属性赋值

System.out.println("UserName:"+rs.getString(0));

}

// 关闭连接

rs.close();

st.close();

conn.close();

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void delete(){

try {

//步骤还是那六个步骤,前边的两步是一样的
String url = "jdbc:oracle:thin:@localhost:1521:XE";

String userName = "system";

String password = "system";

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection conn = DriverManager.getConnection(url,userName,password);

//这里的发送sql语句的对象是PreparedStatement,成为预处理sql对象,因为按条件删除是需要不定值的

String sql = "delete from users where id = ?";

PreparedStatement ps = conn.prepareStatement(sql);

ps.setInt(0, 1);

int row = ps.executeUpdate();

if(row!=0){

System.out.println("删除成功!");

}

// 关闭连接

rs.close();

st.close();

conn.close();

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
我只写了查询和删除,添加、修改和删除非常之像,这是因为查询对数据库没有改动,而增删改都对数据库进行了修改,所以这三个非常像……呵呵,你自己看着办吧

网友(2):

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();

}

}

}

}