Java JDBC篇2——JDBC增删查改
生活随笔
收集整理的這篇文章主要介紹了
Java JDBC篇2——JDBC增删查改
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java JDBC篇2——JDBC增刪查改
url=jdbc:mysql://localhost:3306/test user=root password=blingbling123. driver=com.mysql.jdbc.Driver public class JDBCtool {private static String urls;private static String user;private static String password;private static String driver;static {Properties properties=new Properties();ClassLoader classLoader=JDBCtool.class.getClassLoader();URL url=classLoader.getResource("connection.properties");String path=url.getPath();try {properties.load(new FileReader(path));} catch (IOException e) {e.printStackTrace();}urls=properties.getProperty("url");user=properties.getProperty("user");password=properties.getProperty("password");driver=properties.getProperty("driver");try {Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}public static Connection getconnection() throws SQLException {return DriverManager.getConnection(urls,user,password);}public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){if (resultSet!=null){try {resultSet.close();} catch (SQLException e) {e.printStackTrace();}}if (preparedStatement!=null){try {preparedStatement.close();} catch (SQLException e) {e.printStackTrace();}}if (connection!=null){try {connection.close();} catch (SQLException e) {e.printStackTrace();}}} }1、插入
public class Test {public static void main(String[] args) throws SQLException {Connection connection = JDBCtool.getconnection();String sql="INSERT INTO USER (username, PASSWORD,birthday) VALUES('admin1', '123','2000-12-24')";PreparedStatement preparedStatement = connection.prepareStatement(sql);System.out.println(preparedStatement.executeUpdate());JDBCtool.close(connection,preparedStatement,null);} }2、查詢
public class Test {public static void main(String[] args) throws SQLException {Connection connection = JDBCtool.getconnection();String sql="select * from user";PreparedStatement preparedStatement = connection.prepareStatement(sql);ResultSet resultSet = preparedStatement.executeQuery();while (resultSet.next()){System.out.println(resultSet.getString("username"));}JDBCtool.close(connection,preparedStatement,resultSet);} }3、更新
public class Test {public static void main(String[] args) throws SQLException {Connection connection = JDBCtool.getconnection();String sql="update user set username='yoya' where id=1";PreparedStatement preparedStatement = connection.prepareStatement(sql);System.out.println(preparedStatement.executeUpdate());JDBCtool.close(connection,preparedStatement,null);} }4、刪除
public class Test {public static void main(String[] args) throws SQLException {Connection connection = JDBCtool.getconnection();String sql="delete from user where id in (4,5)";PreparedStatement preparedStatement = connection.prepareStatement(sql);System.out.println(preparedStatement.executeUpdate());JDBCtool.close(connection,preparedStatement,null);} }5、防止SQL注入
public class Test {public static void main(String[] args) throws SQLException {Connection connection = JDBCtool.getconnection();String sql="select * from user where id=?";PreparedStatement preparedStatement = connection.prepareStatement(sql);preparedStatement.setInt(1,1);ResultSet resultSet = preparedStatement.executeQuery();while (resultSet.next()){System.out.println(resultSet.getString("username"));}JDBCtool.close(connection,preparedStatement,resultSet);} }總結
以上是生活随笔為你收集整理的Java JDBC篇2——JDBC增删查改的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL 笔记1 -- 安装MySQL
- 下一篇: Java番外篇3——线程池