Basic JDBC example

String connectionURL = "jdbc:mysql://localhost:3306/test";
		// Change the connection string according to your db, ip, username and password

		Connection con = null;
		try {

			// Load the Driver class.
			Class.forName("com.mysql.jdbc.Driver");
			// If you are using any other database then load the right driver here.

			// Create the connection using the static getConnection method
			con = DriverManager.getConnection(connectionURL, "aaa", "aaaa");

			// Create a Statement class to execute the SQL statement
			Statement stmt = con.createStatement();

			Random random = new Random();
			int status = random.nextInt(2);
			System.out.println(status);
			
			String sql = "update test set status = " + status + " where id = 1";

			stmt.executeUpdate(sql);

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
 

猜你喜欢

转载自z610.iteye.com/blog/1363823