jdbc数据库操作
来源:互联网 发布:pdf在线阅读js插件 编辑:程序博客网 时间:2024/06/17 03:07
package com.jikexueyuan.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCTest {
public static Connection getConnection(){
Connection con = null;
String url = null;
try {
Class.forName("com.mysql.jdbc.Driver");
url ="jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8&useSSL=false";
con = DriverManager.getConnection(url, "root", "201211");
}catch(Exception e) {
e.printStackTrace();
}
return con;
}
public static void insert(){
Connection con = getConnection();
try {
String sql = "INSERT INTO jsp_db.tbl_user (id, name, password, email) VALUES ('3', '张三', '123456', '111111');";
Statement st = con.createStatement();
int count = st.executeUpdate(sql);
System.out.println("用户向表中添加了"+count+"条数据");
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void update(){
Connection con = getConnection();
try {
String sql = "update jsp_db.tbl_user set email='121212' where name = '张三'";
Statement st = con.createStatement();
int count = st.executeUpdate(sql);
System.out.println("用户向表中修改了"+count+"条数据");
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void delete(){
Connection con = getConnection();
try {
String sql = "delete from jsp_db.tbl_user where name = '张三'";
Statement st = con.createStatement();
int count = st.executeUpdate(sql);
System.out.println("用户向表中删除了"+count+"条数据");
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void query(){
Connection con = getConnection();
try {
String sql = "SELECT * FROM jsp_db.tbl_user";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
System.out.print(rs.getInt(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.print(rs.getString(3)+" ");
System.out.print(rs.getString(4)+" ");
System.out.println();
}
rs.close();
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// update();
// insert();
delete();
query();
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCTest {
public static Connection getConnection(){
Connection con = null;
String url = null;
try {
Class.forName("com.mysql.jdbc.Driver");
url ="jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8&useSSL=false";
con = DriverManager.getConnection(url, "root", "201211");
}catch(Exception e) {
e.printStackTrace();
}
return con;
}
public static void insert(){
Connection con = getConnection();
try {
String sql = "INSERT INTO jsp_db.tbl_user (id, name, password, email) VALUES ('3', '张三', '123456', '111111');";
Statement st = con.createStatement();
int count = st.executeUpdate(sql);
System.out.println("用户向表中添加了"+count+"条数据");
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void update(){
Connection con = getConnection();
try {
String sql = "update jsp_db.tbl_user set email='121212' where name = '张三'";
Statement st = con.createStatement();
int count = st.executeUpdate(sql);
System.out.println("用户向表中修改了"+count+"条数据");
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void delete(){
Connection con = getConnection();
try {
String sql = "delete from jsp_db.tbl_user where name = '张三'";
Statement st = con.createStatement();
int count = st.executeUpdate(sql);
System.out.println("用户向表中删除了"+count+"条数据");
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void query(){
Connection con = getConnection();
try {
String sql = "SELECT * FROM jsp_db.tbl_user";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
System.out.print(rs.getInt(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.print(rs.getString(3)+" ");
System.out.print(rs.getString(4)+" ");
System.out.println();
}
rs.close();
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// update();
// insert();
delete();
query();
}
}
0 0
- JDBC 及数据库操作
- JDBC操作数据库类
- JDBC操作数据库汇总
- jdbc操作mysql数据库
- jdbc数据库操作
- JDBC操作数据库--查询
- 使用JDBC操作数据库
- jdbc 操作mysql数据库
- JDBC操作数据库CRUD
- JDBC数据库操作
- Java-Jdbc 数据库操作
- JDBC操作数据库
- JDBC数据库的操作
- JDBC数据库操作
- Java JDBC数据库操作
- JDBC操作数据库
- JDBC操作数据库
- JDBC数据库操作
- 【Mongodb】morphia与spring结合使用详解
- Android之简易天气预报小应用(xml解析练手)
- 优秀的第三方库
- UVA1034423 out of 5
- median-of-two-sorted-arrays
- jdbc数据库操作
- Opencv中视频播放与进度控制
- 单片机 基础概念
- 82.You are managing an Oracle Database 11g database with the following backup strategy: 1) On Sunday
- hdoj1969Pie
- 栈之括号匹配
- c++primer读书笔记:2.5 类型处理
- 5.6 假设有 5 个学生的姓名和分数采用以下方式存储(相同的数组下标对应一个学生 的姓名和分数): char *name[]={"Smith","John","Mary","Havard","Kai
- Js 基础学习之--模拟加入购物车