直接上代码,关于JDBC的解释什么的可以从隔壁看看。
TestJDBC.java
package ggg;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC {
private static String url = "jdbc:mysql://localhost:3306/mydb";
private static String user = "root";
private static String password = "123456";
public static Connection getConnection() throws ClassNotFoundException, SQLException{
Connection conn = null;
//通过工具类获取连接对象
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url, user, password);
return conn;
}
/**
* 增加
*/
public void testInsert(){
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
//3.创建Statement对象
stmt = conn.createStatement();
//4.sql语句
String sql = "insert into student values(7,'杨九',74,86,98)";
//5.执行sql
int count = stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//关闭资源
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
//JdbcUtil.close(conn, stmt);
}
}
// 删除
public void testDelete(){
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
//3.创建Statement对象
stmt = conn.createStatement();
//4.sql语句
String sql = "delete from student where id=7";
//5.执行sql
int count = stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//关闭资源
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
//JdbcUtil.close(conn, stmt);
}
}
/**
* 查询
*/
public void testselect(){
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
//3.创建Statement对象
stmt = conn.createStatement();
//4.sql语句
String sql = "select *from student";
//5.执行sql
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id=rs.getInt("id");
String name=rs.getString("name");
int math=rs.getInt("math");
int English=rs.getInt("English");
int chinese=rs.getInt("chinese");
System.out.println(id+" "+name+" "+math+" "+English+" "+chinese+" ");
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//关闭资源
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
//JdbcUtil.close(conn, stmt);
}
}
/**
* 更新
*/
public void testupdate(){
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
//3.创建Statement对象
stmt = conn.createStatement();
//4.sql语句
String sql = "update student set name='马十' where id=2";
//5.执行sql
int count = stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//关闭资源
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
//JdbcUtil.close(conn, stmt);
}
}
}
sq l文件:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(30) NOT NULL,
`math` tinyint(4) default NULL,
`English` tinyint(4) default NULL,
`chinese` tinyint(4) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `student` VALUES ('1','张三','68','88','90'),
('2','李四','68','88','90'), ('3','王五','45','68','96'),
('4','赵六','75','68','88'), ('5','钱七','78','68','80'),
('6','吴八','77','88','99');