JDBC杩炴帴MySQL鎿嶄綔姝ラ
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC杩炴帴MySQL鎿嶄綔姝ラ相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/state' title='state'>state cal 鍙橀噺 count static aik str
package testJDBC;import java.sql.*;
/**
- @Author 灏忕尓涓婂ぉ
- @Email zhuhuaikuan@gmail.com
- @Data 2019/8/28 14:27
- @Version V1.0
-
@description JDBC 鏌ヨ鏁版嵁搴撶ず渚?br/>*/
public class JDBCdemo1public static void main(String[] args)
Connection conn = null;
Statement statement = null;
ResultSet resultSet = null;
//1.娉ㄥ唽鏁版嵁搴撻┍鍔?br/>tryClass.forName("com.mysql.jdbc.Driver"); //鍔犺浇涓€涓被鍒板唴瀛樹腑,璇ョ被涓湁涓€闈欐€佷唬鐮佸潡,璇ヤ唬鐮佸潡涓湁娉ㄥ唽椹卞姩鐨勪唬鐮? //闈欐€佷唬鐮佸潡浼唬鐮佸涓嬶細DriverManager.registerDriver( new Driver() ) //2.鑾峰彇鏁版嵁搴撹繛鎺? String url = "jdbc:mysql://localhost:3306/" + "jt_db" + "?characterEncoding=utf-8"; /** * jdbc:mysql: 鍗忚鍚? * localhost:3306 涓绘満鍦板潃鍙婄鍙? * jt_db 鏁版嵁搴撳悕 * ?characterEncoding=utf-8 锛?鍙傛暟 */ String user = "root"; //鏁版嵁搴撹繛鎺ョ敤鎴峰悕 String passWord = "123456"; //鏁版嵁搴撹繛鎺ュ瘑鐮? conn = DriverManager.getConnection(url, user, passWord); //3.鑾峰彇浼犺緭鍣? statement = conn.createStatement(); /** * Statement s = con.createStatement(); * PreparedStatement p = con.preparedStatement(); * *PreparedStatement鏄疭tatement鐨勫瓙绫? * 鍙互浣跨敤鍗犱綅绗︼紝鏄缂栬瘧鐨勶紝鎵瑰鐞嗘瘮Statement鏁堢巼楂? * * 渚? * String sql = "update user set password=? where username=?"; * preparedStatement = conn.prepareStatement(sql); * //preparedStatement.setString(绗琋涓棶鍙?浠h〃鐨勫彉閲?; * preparedStatement.setString(1,password); * preparedStatement.setString(2,username); * preparedStatement.executeUpdate(); * */ //4.鎵цsql璇彞,杩斿洖鎵ц缁撴灉 String sql = "select * from account"; /** * executeQuery(); 鐢ㄤ簬浜х敓鍗曚釜缁撴灉闆嗙殑璇彞,琚敤鏉ユ墽琛?SELECT 璇彞,杩斿洖鍊兼槸涓€涓猂esultSet缁撴灉闆嗗璞?鍏朵腑鍖呮嫭sql鏌ヨ鐨勭粨鏋? * executeUpdate(); 鐢ㄤ簬鎵ц INSERT銆乁PDATE 鎴?DELETE 璇彞浠ュ強 SQL DDL锛堟暟鎹畾涔夎瑷€锛夎鍙?杩斿洖涓€涓猧nt鍊?璁板綍鍙楀奖鍝嶈褰曡鏁? */ resultSet = statement.executeQuery(sql); //5.澶勭悊缁撴灉 /** * ResultSet 缁撴灉闆嗗璞? * ResultSet 鏄墽琛屾煡璇㈢被鐨剆ql璇彞鍚庤繑鍥炵殑缁撴灉闆嗗璞?鍏朵腑鍖呮嫭sql鏌ヨ鐨勭粨鏋? * a.閬嶅巻鏁版嵁琛岀殑鏂规硶 * next() 鈥?浣挎寚鍚戞暟鎹鐨勭储寮曞悜涓嬬Щ鍔ㄤ竴琛? * b.鑾峰彇鏁版嵁鐨勬柟娉? * getInt(int columnIndex) * getInt(String columnLable) * getString(int columnIndex) * getString(String columnLable) * getDouble(int columnIndex) * getDouble(String columnLable) * getObject(int columnIndex) * getObject(String columnLable) */ while (resultSet.next()) int id = resultSet.getInt("id"); String name = resultSet.getString("name"); double money = resultSet.getDouble("money"); System.out.println(id + ":" + name + ":" + money); catch (Exception e) e.printStackTrace(); finally //6.閲婃斁璧勬簮 //瓒婃櫄鑾峰彇瓒婂厛閲婃斁锛侊紒 //鏈€濂藉皢閲婃斁璧勬簮浠g爜鏀惧湪finally閲? if(resultSet!=null) try resultSet.close(); catch (SQLException e) e.printStackTrace(); finally resultSet = null; if(statement!=null) try statement.close(); catch (SQLException e) e.printStackTrace(); finally statement = null; if(conn!=null) try conn.close(); catch (SQLException e) e.printStackTrace(); finally conn = null;
以上是关于JDBC杩炴帴MySQL鎿嶄綔姝ラ的主要内容,如果未能解决你的问题,请参考以下文章
koa,express,node閫氱敤鏂规硶杩炴帴MongoDB
鏁版嵁搴撶瑪璁?馃搾 MySQL鍩虹鎿嶄綔鍛戒护 鍒嗙被娓呮櫚