JDBC基础展示
import java. sql. Connection;
import java. sql. DriverManager;
import java. sql. SQLException;
import java. sql. Statement;
public class Main {
private static final String URL = "jdbc:mysql://localhost:3306/student?serverTimezone=UTC" ;
private static final String USERNAME = "yucan" ;
private static final String PASSWORD = "1234" ;
public static void update ( ) {
Connection connection= null ;
Statement statement= null ;
try {
Class. forName ( "com.mysql.cj.jdbc.Driver" ) ;
connection= DriverManager. getConnection ( URL , USERNAME , PASSWORD ) ;
statement= connection. createStatement ( ) ;
String sql= "delete from id where id=1" ;
int count= statement. executeUpdate ( sql ) ;
if ( count> 0 ) {
System. out. println ( "执行成功!" ) ;
}
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
finally {
try {
if ( statement!= null )
statement. close ( ) ;
if ( connection!= null )
connection. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
public static void query ( ) {
Connection connection= null ;
Statement statement= null ;
ResultSet resultSet= null ;
try {
Class. forName ( "com.mysql.cj.jdbc.Driver" ) ;
connection= DriverManager. getConnection ( URL , USERNAME , PASSWORD ) ;
statement= connection. createStatement ( ) ;
String sql= "select id,password from id" ;
resultSet= statement. executeQuery ( sql ) ;
while ( resultSet. next ( ) ) {
String id = resultSet. getString ( "id" ) ;
String password = resultSet. getString ( "password" ) ;
System. out. println ( "id=" + id + ",password=" + password ) ;
}
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
finally {
try {
if ( statement!= null )
statement. close ( ) ;
if ( connection!= null )
connection. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
public static void main ( String[ ] args) {
update ( ) ;
}
}