JDBC操作数据库
1.创建java工程
2.编写代码
0.JDBC工程创建
创建JDBC工程
1.JDBC的基本编程流程
-
2创建数据源 -
2.用JDBC往数据库中增加元素
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) throws SQLException {
Scanner scanner = new Scanner(System.in);
DataSource dataSource=new MysqlDataSource();
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=true");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
System.out.println("请输入一个学号:");
int id = scanner.nextInt();
System.out.println("请输入一个姓名:");
String name = scanner.next();
String sql = "insert into student values(?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
statement.setString(2,name);
System.out.println("statement: " + statement);
int ret = statement.executeUpdate();
System.out.println(ret);
statement.close();
connection.close();
}
}
3.用JDBC修改数据库中的元素
import java.util.Scanner;
public class testJDBCUpdate {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=true");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的id: ");
int id = scanner.nextInt();
System.out.println(" 请输入要修改的name:");
String name = scanner.next();
String sql = "update student set name = ? where id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,name);
statement.setInt(2,id);
System.out.println("statement: " + statement);
int ret = statement.executeUpdate();
System.out.println(ret);
statement.close();
connection.close();
}
}
4.用JDBC删除数据库中的元素
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class TestJDBCDelete {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=true");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
int id = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个删除的id:");
id = scanner.nextInt();
String sql = "delete from student where id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
System.out.println("statement: " + statement);
int ret= statement.executeUpdate();
System.out.println(ret);
statement.close();
connection.close();
}
}
5.用JDBC查询数据库中的元素
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestJDBCSelect {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=true");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
String sql = "select * from student";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("id = " + id + ", name = " + name);
}
resultSet.close();
statement.close();
connection.close();
}
}
|