日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

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

發(fā)布時(shí)間:2023/12/15 数据库 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java jdbc修改_java----jdbc(数据库的添加,删除,修改,更新) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

public class Mtest4Demo {

/*

* jdbc工具類(lèi)

* 提供獲取連接和釋放資源的方法

*/

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;

}

//根據(jù)姓名修改相關(guān)的信息

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("數(shù)據(jù)修改失敗");

}

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}finally {

mtest4Demo.release(conn, pstmt, rSet);

}

}

}

總結(jié)

以上是生活随笔為你收集整理的java jdbc修改_java----jdbc(数据库的添加,删除,修改,更新)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。