文章目录
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<name>Tedu Maven</name>
<mirrorOf>*</mirrorOf>
<!--<url>http://maven.tedu.cn/nexus/content/groups/public</url>-->
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>
<profiles>
</profiles>
<activeProfiles>
</activeProfiles>
</settings>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.tedu</groupId>
<artifactId>JDBCMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
</dependencies>
</project>
execute(sql)
执行DDL create alterexecuteUpdate(sql)
执行DML insert update deleteexecuteQuery(sql)
执行select语句 Class.forName("com.mysql.jdbc.Driver"); //注册驱动
//链接数据库
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "root");
//创建Statement,执行sql语句的对象
Statement statement =connection.createStatement();
String sql_create="create table if not exists t(id int primary key auto_increment,age int ,name varchar(10))";
String sql_insert="insert into t(age,name) values(22,'jack'),(33,'tom')";
String sql_sselect="select * from t";
//执行create 语句
Boolean flag=statement.execute(sql_create);
System.out.println(flag);
//执行insert语句
int row=statement.executeUpdate(sql_insert);
System.out.println(row);
//执行select
ResultSet resultSet=statement.executeQuery(sql_sselect);
while(resultSet.next()){
int id=resultSet.getInt("id");
int age=resultSet.getInt("age");
String name = resultSet.getString("name");
System.out.println(id+"----"+age+"----"+name);
}
Class.forName("com.mysql.jdbc.Driver"); //加载驱动
// 链接数据库
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "root");
Statement statement =connection.createStatement(); //获取执行sql语句对象
String sql_sselect="select * from t"; //创建sql语句
ResultSet resultSet=statement.executeQuery(sql_sselect); //获取结果集
while(resultSet.next()){
int id=resultSet.getInt("id");
int age=resultSet.getInt("age");
String name = resultSet.getString("name");
System.out.println(id+"----"+age+"----"+name);
}
@Test
public void testException() {
Connection connection = null; //申明Connection为null
Statement statement = null; // 申明 Statement为null
try {
Class.forName("com.mysql.jdbc.Driver"); // 注册驱动
// 链接数据库
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jdbc", "root", "root");
// 创建Statement,执行sql语句的对象
statement = connection.createStatement();
String sql_insert = "insert into t(age,name) values(22,'marry'),(33,'Alice')";
int row=statement.executeUpdate(sql_insert);
System.out.println(row);
} catch (Exception e) {
System.out.println("出异常");
e.printStackTrace();
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 数据库封装类
* @author chenjiabing
*/
public class DBUtils {
/**
* 获取连接对象
* @param user 数据库用户名
* @param password 密码
* @param database : 数据库名称
*/
public static Connection getConnection(String user, String password,
String database) throws Exception {
Class.forName("com.mysql.jdbc.Driver"); // 注册驱动
// 链接数据库
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/" + database, user, password);
return connection;
}
/**
* 关闭数据库资源
* @param connection 连接对象
* @param statement Statement对象
* @param resultSet 结果集
*/
public static void close(Connection connection, Statement statement,
ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
@Test
public void testUntils(){
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try {
connection=DBUtils.getConnection("root", "root", "jdbc"); //获取连接
statement=connection.createStatement();
//插入数据
int row = statement.executeUpdate("insert into t(age,name) values(22,'陈加兵'),(33,'Jackson')");
System.out.println(row);
} catch (Exception e) {
e.printStackTrace();
}finally{
DBUtils.close(connection, statement, resultSet);
}
}