代碼如下:
增:
@Test
//數據插入
public void demo1() {
Connection conn=null;
Statement stmt=null;
try {
//注冊驅動
Class.forName("com.mysql.jdbc.Driver");
//創建連接
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/name","root","123456");
//執行sql對象
stmt=conn.createStatement();
String sql="INSERT INTO person VALUES(NULL,'eee','741','小白')";
//返回1個整型
int i=stmt.executeUpdate(sql);
if(i>0) {
System.out.println("插入成功!");
}
}catch(Exception e){
e.printStackTrace();
}finally {
//釋放資源
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn=null;
}
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt=null;
}
}
}
刪:
@Test
//數據刪除
public void demo3() {
Connection conn=null;
Statement stmt=null;
try {
//注冊驅動
Class.forName("com.mysql.jdbc.Driver");
//建立連接
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/name","root","123456");
//創建sql語句
String sql="delete from person where id=5";
stmt=conn.createStatement();
int i=stmt.executeUpdate(sql);
if(i>0) {
System.out.println("刪除成功!");
}
}catch(Exception e) {
e.printStackTrace();
}finally {
//釋放資源
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn=null;
}
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt=null;
}
}
}
改:
@Test
//數據更新
public void demo2() {
Connection conn=null;
Statement stmt=null;
try {
//注冊驅動
Class.forName("com.mysql.jdbc.Driver");
//建立連接
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/name","root","123456");
//創建sql語句
String sql="update person set username='qqq',password='852',address='小陳' where id=3";
//創建sql執行對象
stmt=conn.createStatement();
int i=stmt.executeUpdate(sql);
if(i>0) {
System.out.println("修改成功");
}else {
System.out.println("修改失敗");
}
}catch(Exception e) {
e.printStackTrace();
}finally {
//釋放資源
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn=null;
}
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt=null;
}
}
}
查:
@Test
public void demo2() {
try {
//1.加載驅動
DriverManager.registerDriver(new Driver());
//2.創建連接
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/name", "root", "123456");//是不是自己的賬戶和密碼
//3.1創建sql語句對象
String sql="select * from person";
Statement stmt=conn.createStatement();
//3.2執行sql語句并進行遍歷
ResultSet resultSet=stmt.executeQuery(sql);
while(resultSet.next()) {
int uid=resultSet.getInt("id");
String username=resultSet.getString("username");
String password=resultSet.getString("password");
String address=resultSet.getString("address");
System.out.println(uid+" "+username+" "+password+" "+address+" ");
}
//4.釋放相關資源
resultSet.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
注意:我的數據庫為name,表名為person,用戶名為root,密碼為123456,你需要根據自己的表進行改正。
希望對你有用!
標簽:java,sql,改查,stmt,try,mysql,catch,null,conn
來源: https://blog.csdn.net/hanhanwanghaha/article/details/105728416