application下的JDBC操作

本文详细介绍了使用Java进行数据库操作的方法,包括连接数据库、执行查询、插入、更新和删除记录等核心功能。通过具体代码示例,展示了如何使用PreparedStatement执行参数化查询,提高安全性并防止SQL注入攻击。
摘要由CSDN通过智能技术生成

package com.zss.www;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

public class tableCRUD {

public static void main(String[] args) throws Exception {

/*String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/student";
String username = "root";
String password = "711109";
Connection conn = null;
Class.forName(driver);
conn = (Connection) DriverManager.getConnection(url, username, password);
Statement stmt=null;
ResultSet rs=null;
stmt=(Statement) conn.createStatement();
rs=stmt.executeQuery("select * from information");
while (rs.next()){
System.out.println("name="+rs.getString(1));
System.out.println("name="+rs.getString(2));
}

rs.close();
stmt.close();
conn.close();*/


/*Student one=new Student("mary",10);
selectAll();
update(one);
selectAll();
delete(one);
selectAll();
insert(one);
selectAll();*/
String username01 = "chenjirong";
int age01 = 20;
ResultSet rs = myquery(
"select * from information where name = ? and age = ?",
username01, age01);
while (rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}



username01 = "myfatpig";
age01 = 45;
myinsert(
"insert into information (name,age) values(?,?)",
username01, age01);


}






private static Connection getConn() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/student";
String username = "root";
String password = "711109";
Connection conn = null;
try {
Class.forName(driver);
conn = (Connection) DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}


private static int insert(Student student) {
Connection conn = getConn();
int i = 0;
String sql = "insert into information (name,age) values(?,?)";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}

 


private static int update(Student student) {
Connection conn = getConn();
int i = 0;
String sql = "update information set age="+String.valueOf(student.getAge()+20)+" where name='" + student.getName() + "'";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
i = pstmt.executeUpdate();
System.out.println("resutl: " + i);
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}

 

private static Integer selectAll() {
Connection conn = getConn();
String sql = "select * from information";
System.out.println("************");
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement)conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
int col = rs.getMetaData().getColumnCount();//number of fields
System.out.println("************");
while (rs.next()) {
for (int i = 1; i <= col; i++) {
System.out.print(rs.getString(i) + "\t");
if ((i == 2) && (rs.getString(i).length() < 8)) {
System.out.print("\t");
}
}
System.out.println("");
}
System.out.println("============================");
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

 

private static int delete(Student student) {
Connection conn = getConn();
int i = 0;
String sql = "delete from information where Name='" + student.getName() + "'";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
i = pstmt.executeUpdate();
System.out.println("resutl: " + i);
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}

 

private static ResultSet myquery(String sql, Object... args) throws Exception {

Connection conn = getConn();
PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
pstmt.setObject(i+1, args[i]);
}
return pstmt.executeQuery();
}


private static void myinsert(String sql, Object... args) throws Exception {

Connection conn = getConn();
PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
pstmt.setObject(i+1, args[i]);
}
pstmt.executeUpdate();
pstmt.close();
}

}

 

转载于:https://www.cnblogs.com/bgd150809222/p/6642321.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值