java jdbc 增删改查
来源:互联网 发布:东方的漫画软件 编辑:程序博客网 时间:2024/05/29 17:57
http://www.cnblogs.com/wuyuegb2312/p/3872607.html
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class A {
public static void main(String[] args) {
Student student=new Student("aa", "bb", "26");
//A.insert(student);
//A.update(student);
//A.getAll();
A.delete("aa");
}
private static Connection getConn() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "";
Connection conn = null;
try {
Class.forName(driver); //classLoader,加载对应驱动
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 students (Name,Sex,Age) values(?,?,?)";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getSex());
pstmt.setString(3, 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 students set Age='" + student.getAge() + "' 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 getAll() {
Connection conn = getConn();
String sql = "select * from students";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement)conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
int col = rs.getMetaData().getColumnCount();
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(String name) {
Connection conn = getConn();
int i = 0;
String sql = "delete from students where Name='" + name + "'";
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;
}
}
- JAVA JDBC增删查改
- java jdbc 增删改查
- Jdbc 增删改查
- jdbc增删改查
- JDBC 增删改查
- jdbc增删改查
- jdbc增删查改
- JDBC 增删改查
- jdbc:增删改查
- jdbc---增删改查
- java增删改查JDBC封装类
- java中jdbc增删改查
- java之JDBC增删改查操作
- java JDBC 增删改查 总结
- 【java基础】JDBC实现增删改查
- jdbc的增删改查
- JDBC 增删查改(1)
- JDBC 增删查改(2)
- 最长上升子序列(LIS)
- 基于Tensorflow实现基本的线性回归(Linear regression)
- hdu 6197array array array(最长不下降子序列nlogn)
- 07-异常
- 【React Native】react-Navigation之StackNavigator
- java jdbc 增删改查
- Leetcode OJ 67 Add Binary [Easy]
- C++网络编程之Socket编程
- Java作业-元素检索与交换
- 总结
- wordcloud
- 长连接与短连接
- java中的构造函数
- zTree树插件--树形菜单