一.最原始的使用jdbc连接mysql:
maven依赖只要一个:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
步骤:加载驱动(可以省略)—>获取连接对象—>创建预执行语句对象—>执行sql—>释放连接对象(口诀:贾琏欲执事)代码如下:
public class App
{
public static void main( String[] args )
{
Connection connection=null;
try {
//获取连接对象
connection = DriverManager.getConnection("jdbc:mysql://192.168.50.117:23306/shop?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false", "shop", "shop");
String sql="update shop_order_attr set flg_type=? where cod_order_id=?";
//获取预执行语句
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,30);
preparedStatement.setString(2,"666655555555");
connection.setAutoCommit(false);
//执行sql
boolean execute = preparedStatement.execute();
connection.commit();
if (execute){
System.out.println("成功");
}
} catch (Exception e) {
try {
connection.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}finally {
if(connection!=null){
try {
//释放连接对象
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}