java与MySql连接的增删查改
2008-01-09 10:50
417 查看
package com.tongking;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class TestForMySQL {
/**
* @param args
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
delete("4000");
findAll();
}
public static void findAll() throws ClassNotFoundException, SQLException {
// 1.加载驱动程序。
Class.forName("org.gjt.mm.mysql.Driver");
// 2.建立连接
String url = "jdbc:mysql://localhost/j2ee";
Connection conn = DriverManager.getConnection(url, "root", "root");
// 3.获得语句对象
Statement stmt = conn.createStatement();
// 4.执行SQL
ResultSet rs = stmt.executeQuery("select s_name,s_no from student");
while (rs.next()) {
// System.out.println("学生姓名:" + rs.getString(1));
System.out.println("学生姓名:" + rs.getString("s_name"));
System.out.println("学号:" + rs.getLong("s_no"));
}
// 5.释放资源
stmt.close();
conn.close();
}
public static void findByPk(String pk) throws ClassNotFoundException,
SQLException {
// 1.加载驱动程序。
Class.forName("org.gjt.mm.mysql.Driver");
// 2.建立连接
String url = "jdbc:mysql://localhost/j2ee";
Connection conn = DriverManager.getConnection(url, "root", "root");
// 3.获得语句对象
Statement stmt = conn.createStatement();
// 4.执行SQL
ResultSet rs = stmt
.executeQuery("select s_name,s_no from student where s_no = "
+ pk);
while (rs.next()) {
// System.out.println("学生姓名:" + rs.getString(1));
System.out.println("学生姓名:" + rs.getString("s_name"));
System.out.println("学号:" + rs.getLong("s_no"));
}
// 5.释放资源
stmt.close();
conn.close();
}
public static void insert() throws ClassNotFoundException, SQLException {
// 1.加载驱动程序。
Class.forName("org.gjt.mm.mysql.Driver");
// 2.建立连接
String url = "jdbc:mysql://localhost/j2ee";
Connection conn = DriverManager.getConnection(url, "root", "root");
// 3.获得语句对象
Statement stmt = conn.createStatement();
// 4.执行SQL
int count = stmt
.executeUpdate("insert into student (s_no,s_name,s_birthday,s_password) values (4000,'王五','1998-06-09','12345')");
// 5.释放资源
stmt.close();
conn.close();
}
public static void update(String sno) throws ClassNotFoundException,
SQLException {
// 1.加载驱动程序。
Class.forName("org.gjt.mm.mysql.Driver");
// 2.建立连接
String url = "jdbc:mysql://localhost/j2ee";
Connection conn = DriverManager.getConnection(url, "root", "root");
// 3.获得语句对象
Statement stmt = conn.createStatement();
// 4.执行SQL
int count = stmt
.executeUpdate("update student set s_name = '东流',s_birthday = '1998-06-02',s_password ='234' where s_no = "
+ sno);
// 5.释放资源
stmt.close();
conn.close();
}
public static void delete(String sno) throws ClassNotFoundException,
SQLException {
// 1.加载驱动程序。
Class.forName("org.gjt.mm.mysql.Driver");
// 2.建立连接
String url = "jdbc:mysql://localhost/j2ee";
Connection conn = DriverManager.getConnection(url, "root", "root");
// 3.获得语句对象
Statement stmt = conn.createStatement();
// 4.执行SQL
int count = stmt.executeUpdate("delete from student where s_no = "
+ sno);
// 5.释放资源
stmt.close();
conn.close();
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class TestForMySQL {
/**
* @param args
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
delete("4000");
findAll();
}
public static void findAll() throws ClassNotFoundException, SQLException {
// 1.加载驱动程序。
Class.forName("org.gjt.mm.mysql.Driver");
// 2.建立连接
String url = "jdbc:mysql://localhost/j2ee";
Connection conn = DriverManager.getConnection(url, "root", "root");
// 3.获得语句对象
Statement stmt = conn.createStatement();
// 4.执行SQL
ResultSet rs = stmt.executeQuery("select s_name,s_no from student");
while (rs.next()) {
// System.out.println("学生姓名:" + rs.getString(1));
System.out.println("学生姓名:" + rs.getString("s_name"));
System.out.println("学号:" + rs.getLong("s_no"));
}
// 5.释放资源
stmt.close();
conn.close();
}
public static void findByPk(String pk) throws ClassNotFoundException,
SQLException {
// 1.加载驱动程序。
Class.forName("org.gjt.mm.mysql.Driver");
// 2.建立连接
String url = "jdbc:mysql://localhost/j2ee";
Connection conn = DriverManager.getConnection(url, "root", "root");
// 3.获得语句对象
Statement stmt = conn.createStatement();
// 4.执行SQL
ResultSet rs = stmt
.executeQuery("select s_name,s_no from student where s_no = "
+ pk);
while (rs.next()) {
// System.out.println("学生姓名:" + rs.getString(1));
System.out.println("学生姓名:" + rs.getString("s_name"));
System.out.println("学号:" + rs.getLong("s_no"));
}
// 5.释放资源
stmt.close();
conn.close();
}
public static void insert() throws ClassNotFoundException, SQLException {
// 1.加载驱动程序。
Class.forName("org.gjt.mm.mysql.Driver");
// 2.建立连接
String url = "jdbc:mysql://localhost/j2ee";
Connection conn = DriverManager.getConnection(url, "root", "root");
// 3.获得语句对象
Statement stmt = conn.createStatement();
// 4.执行SQL
int count = stmt
.executeUpdate("insert into student (s_no,s_name,s_birthday,s_password) values (4000,'王五','1998-06-09','12345')");
// 5.释放资源
stmt.close();
conn.close();
}
public static void update(String sno) throws ClassNotFoundException,
SQLException {
// 1.加载驱动程序。
Class.forName("org.gjt.mm.mysql.Driver");
// 2.建立连接
String url = "jdbc:mysql://localhost/j2ee";
Connection conn = DriverManager.getConnection(url, "root", "root");
// 3.获得语句对象
Statement stmt = conn.createStatement();
// 4.执行SQL
int count = stmt
.executeUpdate("update student set s_name = '东流',s_birthday = '1998-06-02',s_password ='234' where s_no = "
+ sno);
// 5.释放资源
stmt.close();
conn.close();
}
public static void delete(String sno) throws ClassNotFoundException,
SQLException {
// 1.加载驱动程序。
Class.forName("org.gjt.mm.mysql.Driver");
// 2.建立连接
String url = "jdbc:mysql://localhost/j2ee";
Connection conn = DriverManager.getConnection(url, "root", "root");
// 3.获得语句对象
Statement stmt = conn.createStatement();
// 4.执行SQL
int count = stmt.executeUpdate("delete from student where s_no = "
+ sno);
// 5.释放资源
stmt.close();
conn.close();
}
}
相关文章推荐
- Java学习笔记——JDBC之与数据库MySQL的连接以及增删改查等操作
- Java连接MySQL实现增删改查
- JAVA连接到MySql,并进行增删改查操作
- Java语句轻松实现与数据库MYSQL【本地数据库】的连接,和对数据库的增删改查操作
- java连接mysql与简单操作mysql的增删改查简单例子
- java连接mysql增删改查测试通过
- java连接mysql实现增删改查代码
- Java学习-006-三种数据库连接 MySQL、Oracle、sqlserver
- win7安装MySQL并用java连接
- MySQL配置SSL之后的JAVA连接MySQL的代码
- 【Mysql】Java中对Mysql数据库的增删改查、Java的System类
- JAVA连接MYSQL通过查询返回的结果集获取表结构字段类型
- php连接mysql 留言板增删改查完整小案例
- IDEA连接mysql教程,以及出现java.lang.ClassNotFoundException: com.mysql.jdbc.Driver的解决办法
- Java连接mySql——简单JDBC连接数据库
- 怎么在官网上下载java连接mysql的驱动jar包?
- JAVA连接mysql
- Hive连接MYSQL数据OK,查询出现in thread "Thread-18" java.lang.IllegalArgumentException:Does not contain a vali
- Java连接Mysql数据库增删改查操作
- MySql c# 连接 mysql 以及实现简易增删改查