200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > java jdbc修改_java----jdbc(数据库的添加 删除 修改 更新)

java jdbc修改_java----jdbc(数据库的添加 删除 修改 更新)

时间:2020-10-23 01:47:35

相关推荐

java jdbc修改_java----jdbc(数据库的添加 删除 修改 更新)

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

public class Mtest4Demo {

/*

* jdbc工具类

* 提供获取连接和释放资源的方法

*/

public static Connection getConnection() throws ClassNotFoundException {

Connection conn=null;

try {

Class.forName("com.mysql.jdbc.Driver");

conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/study","root","root");

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

return conn;

}

public static void release(Connection conn,PreparedStatement pstmt,ResultSet rSet) {

if(rSet!=null)

{

try {

rSet.close();

} catch (Exception e) {

e.printStackTrace();

}

}

if(conn!=null)

{

try {

conn.close();

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

if(pstmt!=null)

{

try {

pstmt.close();

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

}

//添加信息

public int testAdd(String sqlString) {

Connection conn=null;

PreparedStatement pstmt=null;

Mtest4Demo mtest4Demo=new Mtest4Demo();

try {

conn=mtest4Demo.getConnection();

pstmt=conn.prepareStatement(sqlString);

int row=pstmt.executeUpdate();

if(row>0)

{

return 1;

}else {

return 0;

}

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

return 0;

}

public int testDeleteByName(String username) {

Connection conn=null;

PreparedStatement pstmt=null;

Mtest4Demo mtest4Demo=new Mtest4Demo();

try {

conn=mtest4Demo.getConnection();

String sqlString="delete from login where "+"username=‘"+username+"‘";

pstmt=conn.prepareStatement(sqlString);

int row=pstmt.executeUpdate();

if(row>0)

{

return 1;

}else {

return 0;

}

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

return 0;

}

//根据姓名修改相关的信息

public int UpdateByName(String name,String username,String password) {

Connection conn=null;

PreparedStatement pstmt=null;

Mtest4Demo mtest4Demo=new Mtest4Demo();

try {

conn=mtest4Demo.getConnection();

String sqlString="update login set username=?,password=? where username=?";

pstmt=conn.prepareStatement(sqlString);

pstmt.setString(1, username);

pstmt.setString(2, password);

pstmt.setString(3, name);

int row=pstmt.executeUpdate();

if(row>0)

{

return 1;

}else {

return 0;

}

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

return 0;

}

}

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class Mtest5Demo {

public static void main(String[] args) throws ClassNotFoundException, SQLException {

Mtest4Demo mtest4Demo=new Mtest4Demo();

Connection conn=null;

PreparedStatement pstmt=null;

ResultSet rSet=null;

try {

conn=mtest4Demo.getConnection();

String sqlString="select *from login";

pstmt=conn.prepareStatement(sqlString);

rSet=pstmt.executeQuery();

while(rSet.next())

{

System.out.println(rSet.getString("username")+"---"+rSet.getString("password"));

}

String sqlString2="insert into login values (‘test‘,‘test‘)";

int row=mtest4Demo.testAdd(sqlString2);

if(row==1)

System.out.println("信息添加成功");

else {

System.out.println("信息添加失败");

}

String nameString="zyz";

int delete=mtest4Demo.testDeleteByName(nameString);

if(delete==1) {

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

}else {

System.out.println("删除失败");

}

String name="test";

String username="1234567890";

String password="1234567890";

int update=mtest4Demo.UpdateByName(name, username, password);

if(update==1) {

System.out.println("信息修改成功");

}else {

System.out.println("数据修改失败");

}

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}finally {

mtest4Demo.release(conn, pstmt, rSet);

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。