import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String driver;
private static String url;
private static String username;
private static String password;
//静态
static {
InputStream is = null;
try {
is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(is);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, username, password);
return connection;
}
public static void clo(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
}
import org.junit.Test;
import java.sql.*;
import java.util.Scanner;
public class j003wen {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
j003wen j003wen = new j003wen();
j003wen.chaid();
}
public void deng() throws SQLException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.println("用户名:");
String users = scanner.next();
System.out.println("密码:");
String password = scanner.next();
String users_password_sql = "select * from upss where users=? and password=?";
Connection connection = Jdbcut.getConnection();
PreparedStatement statement = connection.prepareStatement(users_password_sql);
statement.setString(1, users);
statement.setString(2, password);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
System.out.println("登录成功!");
} else {
System.out.println("登录失败!");
}
Jdbcut.clo(resultSet, statement, connection);
}
public void addtext() throws ClassNotFoundException, SQLException {
Scanner scanner = new Scanner(System.in);
System.out.println("id:");
int id = scanner.nextInt();
System.out.println("name:");
String name = scanner.next();
System.out.println("price:");
int price = scanner.nextInt();
System.out.println("shu:");
int shu = scanner.nextInt();
String addsql = "insert into shang (id,name,price,shu) values (?,?,?,?)";
Connection connection = JDBCUtils.getConnection();
PreparedStatement statement = connection.prepareStatement(addsql);
statement.setInt(1, id);
statement.setString(2, name);
statement.setInt(3, price);
statement.setInt(4, shu);
statement.executeUpdate();
JDBCUtils.clo(null, statement, connection);
}
public static void shan() throws SQLException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.println("id:");
int id = scanner.nextInt();
String shansql = "delete from shang where id = ?";
Connection connection = JDBCUtils.getConnection();
PreparedStatement statement = connection.prepareStatement(shansql);
statement.setInt(1, id);
statement.executeUpdate();
JDBCUtils.clo(null, statement, connection);
}
public static void gai() throws SQLException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.println("name:");
String name = scanner.next();
System.out.println("price:");
int price = scanner.nextInt();
System.out.println("shu:");
int shu = scanner.nextInt();
System.out.println("id:");
int id = scanner.nextInt();
String gaisql = "update shang set name = ? ,price = ?, shu = ? where id = ? ";
Connection connection = JDBCUtils.getConnection();
PreparedStatement statement = connection.prepareStatement(gaisql);
statement.setString(1, name);
statement.setInt(2, price);
statement.setInt(3, shu);
statement.setInt(4, id);
statement.executeUpdate();
JDBCUtils.clo(null, statement, connection);
}
public static void cha() throws SQLException, ClassNotFoundException {
String gaisql = "select * from shang";
Connection connection = JDBCUtils.getConnection();
PreparedStatement statement = connection.prepareStatement(gaisql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int price = resultSet.getInt("price");
int shu = resultSet.getInt("shu");
System.out.println(id + ":" + name + ":" + price + ":" + shu);
}
JDBCUtils.clo(resultSet, statement, connection);
}
public static void chaid() throws SQL
Exception, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.println("id:");
int id = scanner.nextInt();
String gaisql = "select * from shang where id = " + id;
Connection connection = JDBCUtils.getConnection();
PreparedStatement statement = connection.prepareStatement(gaisql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int id1 = resultSet.getInt("id");
String name = resultSet.getString("name");
int price = resultSet.getInt("price");
int shu = resultSet.getInt("shu");
System.out.println(id1 + ":" + name + ":" + price + ":" + shu);
}
JDBCUtils.clo(resultSet, statement, connection);
}
}
、