java jdbc原始数据库操作
1,需要创建实体类
2,导入jar包
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
</dependencies>
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
public class test {
@Test
public void jdbc() throws Exception{
Connection connection = null;
PreparedStatement preparedStatement = null;
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
String url ="jdbc:mysql://127.0.0.1:3306/test";
String username = "root";
String paaaword = "root";
connection = DriverManager.getConnection(url, username, paaaword);
//3.获取statement,preparedStatement
String sql = "Update account set money=? where name=?";
preparedStatement = connection.prepareStatement(sql);
//4.设置参数,这边的第一个参数代表了id=,第二个参数代表了id的值,如果查询条件有两个则第二个parameterIndex写2
preparedStatement.setDouble(1,1000.0);
preparedStatement.setString(2, "张三");
//5.执行查询,获取结果集
int resultSet = preparedStatement.executeUpdate();
//6.处理结果集
//7.关闭连接,释放资源
preparedStatement.close();
connection.close();
}
}